Solved

Java Jpanels and Jframe

Posted on 2016-11-16
8
19 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
Comment Utility
0
 

Author Comment

by:MarkLoveExEx
Comment Utility
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
Comment Utility
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
 
LVL 16

Accepted Solution

by:
krakatoa earned 500 total points
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

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

Expert Comment

by:krakatoa
Comment Utility
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
Comment Utility
You were correct.  I had to be more careful on the order of my constructors...  Thanks, Mark
0
 
LVL 16

Expert Comment

by:krakatoa
Comment Utility
Very pleased to hear you got there.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now