Solved

Java Jpanels and Jframe

Posted on 2016-11-16
8
32 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

776 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