Solved

Java Jpanels and Jframe

Posted on 2016-11-16
8
37 Views
Last Modified: 2016-11-17
I am completely disgusted right now.

I have a JFrame, and several complex JPanels that I want added to that JFrame, but I keep getting Null Exception errors.

I'm about to give up.
0
Comment
Question by:MarkLoveExEx
  • 4
  • 3
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 41889531
0
 

Author Comment

by:MarkLoveExEx
ID: 41889568
Let me try to post some code segments.
All my programs start with a class called "StartHere":
In StartHere (below the class, but above "main", I declare a JFrame and 2 JPanels:
public static TopLevelFrame myTopLevelFrame;
public static BasinSelectorPanel myBasinSelectorPanel;
public static UpdateSegmentPanel myUpdateSegmentPanel;

Within main,
myTopLevelFrame = new TopLevelFrame();
myTopLevelFrame.setVisible(true);
myBasinSelectorPanel = new BasinSelectorPanel();
//myBasinSelectorPanel.setVisible(true);
myUpdateSegmentPanel = new UpdateSegmentPanel();             ****Fails****, with Exception in thread "main" java.lang.NullPointerException
//myUpdateSegmentPanel.setVisible(true);

Now, for my TopLevelFrame JFrame:
public class TopLevelFrame extends JFrame implements ActionListener {
JTabbedPane tabbedPane = new JTabbedPane();
}

public TopLevelFrame() throws Exception {
super blah blah
setSize blah blah
add(tabbedPane);

These seem to work.  I get the tabs on the TopLevelFrame, but they have no contents
tabbedPane.addTab("LAST UPDATE", StartHere.myUpdateSegmentPanel);
tabbedPane.addTab("VIEW HYDROGRAPHS", StartHere.myBasinSelectorPanel);

Now, my UpdateSegmentPanel:
public class UpdateSegmentPanel extends JPanel implements ActionListener, ListSelectionListener {

DefaultListModel<String> locationId_model_Chowan = new DefaultListModel<String>();
JList<String> JList_Chowan = new JList<String>(locationId_model_Chowan);

public UpdateSegmentPanel() throws Exception {
StartHere.myUpdateSegmentPanel.add(new JScrollPane(JList_Chowan));    ****Fails****, I think the NullPointerException is coming from here
}
0
 

Author Comment

by:MarkLoveExEx
ID: 41889587
I've also tried to create a new Jpanel within TopLevelFrame:
JPanel panelHydrographViewer = new JPanel();

Then, add the StartHere.myBasinSelectorPanel to THAT jPanel.
I can't add it. Another Null Pointer Exception
0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 16

Accepted Solution

by:
krakatoa earned 500 total points
ID: 41889588
As your code is not properly presented - i.e. inside code tags here on EE -  and difficult to see how the snippets fit together, it is nigh impossible to say what's going on. But one of the first things I would look at is whether you are calling the constructors in the right order - i.e are you sure you have an "myUpdateSegmentPanel" ready to "add()" a component to - as that would be a prime candidate for an NPE.
0
 

Author Comment

by:MarkLoveExEx
ID: 41889590
Should the constructors of the panels be constructed before the main JFrame that holds them?
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 41889666
Well if you try to add something to a component that doesn't exist, that will fail, as would adding something that doesn't exist to a component that does.

( I should add that your JFrame is probably not the one at fault, since if it isn't assigned then the compiler will tell you).
0
 

Author Closing Comment

by:MarkLoveExEx
ID: 41892005
You were correct.  I had to be more careful on the order of my constructors...  Thanks, Mark
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 41892015
Very pleased to hear you got there.
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question