Solved

Java Jpanels and Jframe

Posted on 2016-11-16
8
46 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

742 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