0

I want a temporary loading screen in the transition from one frame to another. In the mainframe I create the loading screen which creates the other screen (employmentframe). It only creates it, it does not show it yet.

In the employment frame I have put some loadingframe.setloadingbar() methods which call the setloadingbar method in loadingframe. This works perfect until it reaches 100. At getvalue() == 100 it should set the employmentframe visible, but instead it gives me a nullpointerexception. Which is weird because the employment-screen IS created.

The code is below -

Employmentframe:

   public EmploymentFrame(int eid, JFrame thisframe) {         
        initComponents();
        //loadCaseFileList();
        e_id = eid;
        loadCourseList();
        EmploymentFrame.thisframe = thisframe;
        LoadingFrame.setLoadingBar(1);
    }
    public static void setEmploymentFrameVisible()
    {
       thisframe.setVisible(true);
    }

The loadingframe:

private static JFrame Employmentframe;
private static int oldvalue;
private int e_id;
public LoadingFrame(int type, int eid) {
    initComponents();
    this.e_id = eid;
    if(type == 1)
    {
        Employmentframe = new EmploymentFrame(eid, Employmentframe); 
    }
}

   public static void setLoadingBar(int load)
   {
       oldvalue = LoadingBar.getValue();
       System.out.println(""+oldvalue);
       int newvalue = oldvalue+load;
       System.out.println("nv"+newvalue);
       LoadingBar.setValue(newvalue);
       if(LoadingBar.getValue() == 100)
       {
           EmploymentFrame.setEmploymentFrameVisible();
       }
   }

Thanks.

Reimeus
  • 158,255
  • 15
  • 216
  • 276
Kraishan
  • 443
  • 5
  • 14
  • 38
  • Can you post the exception to see where is being thrown. – madth3 Jan 27 '13 at 02:42
  • Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at nypdapp.EmploymentFrame.setEmploymentFrameVisible(EmploymentFrame.java:36) at nypdapp.LoadingFrame.setLoadingBar(LoadingFrame.java:39) – Kraishan Jan 27 '13 at 11:40

1 Answers1

1

The stacktrace indicates that this line is throwing the NPE

thisframe.setVisible(true);

so thisframe is null.

When you create Employmentframe here

Employmentframe = new EmploymentFrame(eid, Employmentframe); 

you are passing in null as an argument to the constructor as the JFrame has not yet been initialized. In fact, the EmploymentFrame does not need to be passed an instance of itself.


There are a number of other issues:

  • Static methods are seen as a poor design choice in any OO language
  • Multiple JFrames are considered difficult to manage. Preferred alternatives are 1.) CardLayout on a single JFrame or 2.) In cases where multiple windows are required a single JFrame with a modal JDialog can be used. Also discussed here.
  • Code conventions in Java indicate that variables should begin with a lowercase letter.
Community
  • 1
  • 1
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • So what I have done right now is add an internal frame with the loading stuff in it and make it visible when pressed on the empbutton. then inside the empbutton's code it does: newframe = new EmploymentFrame(this_eid); NewFrame is a static private jframe declared at the top of the code. But then still it is null when called newframe.setvisible(true); at the loadingbar's code. How? – Kraishan Jan 27 '13 at 13:04
  • It's difficult to to comment without seeing the code. Consider posting an [SSCCE](http://sscce.org/). Again `static` methods are poor, lead to problems such as this. – Reimeus Jan 27 '13 at 13:54
  • I dont see another way to use a other-class method without it being a static one (excuse me for being an amateur). `code`private static int oldvalue; private static JFrame newframe; public static void setLoadingBar(int load) { oldvalue = LoadingBar.getValue(); int newvalue = oldvalue+load; LoadingBar.setValue(newvalue); if(LoadingBar.getValue() == 100) { newframe.setVisible(true); } } public EmploymentFrame(int eid) { initComponents(); //MainFrame.setLoadingBar(100); } – Kraishan Jan 27 '13 at 14:09
  • Here's a link to a codepaste page where I pasted the code. Im new to this site, so please excuse me. [link]http://codepaste.net/rdb3ts – Kraishan Jan 27 '13 at 14:16
  • I would say to paste to the whole code to that site. Snippets are not really helpful. – Reimeus Jan 27 '13 at 14:18