Solved

Java Jpanels and Jframe

Posted on 2016-11-16
8
41 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
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!

 
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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…

679 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