473,386 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Java GUI Static component, NullPointerException

37
Hey everyone, I created a GUI project, where I'm using a JTree. When it's created everything is ok, but in my run() method when I call it i get a nullPointerException. Here is the code.
Expand|Select|Wrap|Line Numbers
  1. treePanel = new javax.swing.JPanel();
  2. DefaultMutableTreeNode top =
  3.         new DefaultMutableTreeNode("blah");
  4.         mytree = new javax.swing.JTree(top);
  5. javax.swing.GroupLayout treePanelLayout = new javax.swing.GroupLayout(treePanel);
  6.         treePanel.setLayout(treePanelLayout);
  7.         treePanelLayout.setHorizontalGroup(
  8.             treePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  9.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, treePanelLayout.createSequentialGroup()
  10.                 .addContainerGap()
  11.                 .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  12.             .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 138, Short.MAX_VALUE)
  13.         );
  14.         treePanelLayout.setVerticalGroup(
  15.             treePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  16.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, treePanelLayout.createSequentialGroup()
  17.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE)
  18.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  19.                 .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  20.         );
  21.  
of course I add stuff to the tree, but i'm not showing it here cause it's part of the project and personal data.

and in run() method

Expand|Select|Wrap|Line Numbers
  1. mPanel= new JPanel();
  2. mPanel.setLayout(new BoxLayout(mPanel,BoxLayout.X_AXIS));
  3. mPanel.add(treePanel);
  4.  
and the declaration of my variables
Expand|Select|Wrap|Line Numbers
  1.     // Variables declaration - do not modify
  2.     private javax.swing.JComboBox jComboBox1;
  3.     private javax.swing.JScrollPane jScrollPane1;
  4.     private javax.swing.JTree mytree;
  5.     private javax.swing.JPanel treePanel;
  6.     // End of variables declaration
  7.  
Jan 7 '11 #1

✓ answered by horace1

it is difficult not being able to see all the code

is your main() method in a class call Main?
if so could you do
Expand|Select|Wrap|Line Numbers
  1. public static void main(String args[]) {
  2.    Main x = new Main();
  3.  
to create an instance of Main which would call initComponents();

10 3067
horace1
1,510 Expert 1GB
when you get the exception does it tell you which line in the code it is on?
Jan 7 '11 #2
AlarV
37
well yeah it says:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at charybdis.Main$2.run(Main.java:260)
and it is this line
Expand|Select|Wrap|Line Numbers
  1. mPanel.add(treePanel);
Jan 7 '11 #3
horace1
1,510 Expert 1GB
it looks like treePanel does not reference a JPanel object hence you get a NullPointerException
have you a
Expand|Select|Wrap|Line Numbers
  1. treePanel=new JPanel();
  2.  
in your code ? is it before you call the run() method ?
Jan 7 '11 #4
AlarV
37
yes it's on line 1 in the first code tag!
Jan 7 '11 #5
horace1
1,510 Expert 1GB
looks like you have not executed that code before you call the run() method
as a quick test try in the declarations
Expand|Select|Wrap|Line Numbers
  1. private javax.swing.JPanel treePanel = new javax.swing.JPanel();
  2.  
Jan 7 '11 #6
AlarV
37
well the main program has this method
Expand|Select|Wrap|Line Numbers
  1. public Main() {
  2.         initComponents();
  3.     }
and then in initComponents it does all the work I posted. then there is this

Expand|Select|Wrap|Line Numbers
  1. public static void main(String args[]) {
  2.         java.awt.EventQueue.invokeLater(new Runnable() {
  3.  
  4.             public void run() {
  5.                 try {
and that's where I add the panel to the main frame.

I can't try what you suggested because the variable declaration part is locked and I can't add stuff!
If the code isn't executed the problem is why, as the initComponents runs even before the main() method..
Jan 7 '11 #7
horace1
1,510 Expert 1GB
when do you call method Main() ?
try calling initComponents(); in main() before you call run() ?
Jan 7 '11 #8
AlarV
37
I can't cause of the classic bull#*$@#* of java.

initComponents isn't static and I can't call it in static main..

I tried what you suggested before though.
treePanel=new JPanel(); before run() and it worked!
this means that initComponents doesn't run, so I'll have to check this out.

If you have any ideas why initComponents is not working you are more than welcome to tell them. I'm stuck at this for like 2 days..
Jan 7 '11 #9
horace1
1,510 Expert 1GB
it is difficult not being able to see all the code

is your main() method in a class call Main?
if so could you do
Expand|Select|Wrap|Line Numbers
  1. public static void main(String args[]) {
  2.    Main x = new Main();
  3.  
to create an instance of Main which would call initComponents();
Jan 7 '11 #10
AlarV
37
sorry for the delay.. you are right! that was the problem I thought that as it was in the class it ran alone, but you have to call it in main.

Thanx!
Jan 7 '11 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Smith | last post by:
The program compiled successfully, but it gives the following error on runtime.... java.lang.NullPointerException at FrogManiaApp.paint(FrogManiaApp.java:102) at...
2
by: Mike | last post by:
..NET framework 1.2 Hello. We've been accessing a Java COM component from classic ASP for a while now, which works fine. We now need to access it from .NET, but we have no idea how to...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
17
oll3i
by: oll3i | last post by:
nevermind the previous prob i posted i m getting further and i get a new exception in readFile method pleae help me with that one thank u a lot in advance java.lang.NullPointerException...
0
oll3i
by: oll3i | last post by:
package library.common; import java.sql.ResultSet; public interface LibraryInterface { public ResultSet getBookByAuthor(String author); public ResultSet getBookByName(String name);
3
oll3i
by: oll3i | last post by:
double dystanse = null; Integer czyNajblizszySasiad = null; Integer najblizszySasiad=null; System.out.println("Przed 1 petla for"); ...
7
helpwithcode
by: helpwithcode | last post by:
Hi people, I am just learning java.I have been creating a project which involves JDBC Connectivity.I find that the statements, String string_dob=text_dob.getText(); //Converting string to...
1
by: Lost Prophet | last post by:
Hi Im having a problem with some code. Originally, the application was set into three packages (script, gui and control). I have changed is so there is a fourth called 'report'. Whenever I call...
2
by: dragonridingsorceress | last post by:
I am trying to learn how to use the JFileChooser. I'm working in BlueJ. I keep getting a NullPointerException. Full text of the error message is at the bottom of the post. My code is based on some...
1
by: r035198x | last post by:
This exception occurs often enough in practice to warrant its own article. It is a very silly exception to get because it's one of the easiest exceptions to avoid in programming. Yet we've all got it...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.