Results 1 to 3 of 3
Thread: NullPointerException Problems
- 01-20-2012, 05:28 AM #1
Member
- Join Date
- Jan 2012
- Posts
- 48
- Rep Power
- 0
NullPointerException Problems
Hi, i am new to java and having some errors when running the program.
Here is the code:
And here is the error message :Java Code:package OOPJ.ui; import javax.swing.JPanel; import java.awt.Color; import java.awt.Dimension; import javax.swing.JLabel; import java.awt.Rectangle; import javax.swing.ImageIcon; import javax.swing.SwingConstants; import java.awt.Point; import javax.swing.JButton; import Signup.entity.myApp; import Signup.entity.SignUp; import OOPJ.ui.LoginPanel; import OOPJ.ui.MainFrame; public class MenuPanel extends JPanel { private static final long serialVersionUID = 1L; private JLabel jLabelMenu = null; private JButton jButtonProfile = null; private JButton jButton1 = null; private JButton jButton2 = null; private JButton jButton3 = null; private JButton jButton4 = null; private JLabel jLabelLogo = null; private JButton jButton5 = null; private JLabel jLabelHello = null; private MainFrame myFrame = null; /** * This is the default constructor */ public MenuPanel() { super(); initialize(); } String username = null; public MenuPanel(MainFrame f) { this(); myFrame = f; // find out the current login user SignUp curUser = myFrame.getApp().getCurLoginUser(); // set the name jLabelHello.setText("Hello " + curUser.getUsername() ); //line 53: this is where the error are at. } /** * This method initializes this * * @return void */ private void initialize() { jLabelHello = new JLabel(); jLabelHello.setBounds(new Rectangle(16, 87, 80, 27)); jLabelHello.setText("Hello,"); jLabelLogo = new JLabel(); jLabelLogo.setText("JLabel"); jLabelLogo.setSize(new Dimension(136, 56)); jLabelLogo.setIcon(new ImageIcon(getClass().getResource("/images/ui/images/logo.jpg"))); jLabelLogo.setLocation(new Point(12, 16)); jLabelMenu = new JLabel(); jLabelMenu.setBounds(new Rectangle(145, 37, 451, 108)); jLabelMenu.setIcon(new ImageIcon(getClass().getResource("/images/ui/images/menutitle.png"))); jLabelMenu.setHorizontalAlignment(SwingConstants.LEADING); jLabelMenu.setHorizontalTextPosition(SwingConstants.CENTER); jLabelMenu.setText(""); this.setSize(700, 500); this.setLayout(null); this.setBackground(Color.white); this.add(jLabelMenu, null); this.add(getJButtonProfile(), null); this.add(getJButton1(), null); this.add(getJButton2(), null); this.add(getJButton3(), null); this.add(jLabelLogo, null); this.add(getJButton4(), null); this.add(getJButton5(), null); this.add(jLabelHello, null); } /** * This method initializes jButtonProfile * * @return javax.swing.JButton */ private JButton getJButtonProfile() { if (jButtonProfile == null) { jButtonProfile = new JButton(); jButtonProfile.setBounds(new Rectangle(22, 439, 120, 37)); jButtonProfile.setHorizontalAlignment(SwingConstants.LEFT); jButtonProfile.setHorizontalTextPosition(SwingConstants.LEFT); jButtonProfile.setText("Back To Profile"); jButtonProfile.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { JPanel panel = new UserProfile(myFrame); myFrame.getContentPane().removeAll(); myFrame.getContentPane().add(panel); myFrame.getContentPane().validate(); myFrame.getContentPane().repaint(); } }); } return jButtonProfile; } /** * This method initializes jButton1 * * @return javax.swing.JButton */ private JButton getJButton1() { if (jButton1 == null) { jButton1 = new JButton(); jButton1.setLocation(new Point(16, 282)); jButton1.setIcon(new ImageIcon(getClass().getResource("/images/ui/images/Picture1.png"))); jButton1.setSize(new Dimension(180, 105)); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { JPanel panel = new ForumPanel(myFrame); myFrame.getContentPane().removeAll(); myFrame.getContentPane().add(panel); myFrame.getContentPane().validate(); myFrame.getContentPane().repaint(); } }); } return jButton1; } /** * This method initializes jButton2 * * @return javax.swing.JButton */ private JButton getJButton2() { if (jButton2 == null) { jButton2 = new JButton(); jButton2.setLocation(new Point(16, 160)); jButton2.setIcon(new ImageIcon(getClass().getResource("/images/ui/images/Picture2.png"))); jButton2.setBackground(Color.white); jButton2.setSize(new Dimension(180, 105)); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { JPanel panel = new CancerPanel(myFrame); myFrame.getContentPane().removeAll(); myFrame.getContentPane().add(panel); myFrame.getContentPane().validate(); myFrame.getContentPane().repaint(); } }); } return jButton2; } /** * This method initializes jButton3 * * @return javax.swing.JButton */ private JButton getJButton3() { if (jButton3 == null) { jButton3 = new JButton(); jButton3.setLocation(new Point(203, 158)); jButton3.setIcon(new ImageIcon(getClass().getResource("/images/ui/images/journey.jpg"))); jButton3.setSize(new Dimension(294, 227)); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { JPanel panel = new KBSPanel(myFrame); myFrame.getContentPane().removeAll(); myFrame.getContentPane().add(panel); myFrame.getContentPane().validate(); myFrame.getContentPane().repaint(); } }); } return jButton3; } /** * This method initializes jButton4 * * @return javax.swing.JButton */ private JButton getJButton4() { if (jButton4 == null) { jButton4 = new JButton(); jButton4.setIcon(new ImageIcon(getClass().getResource("/images/ui/images/Picture4.png"))); jButton4.setLocation(new Point(504, 160)); jButton4.setSize(new Dimension(180, 105)); jButton4.setBackground(Color.white); jButton4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { JPanel panel = new OrganicPanel(myFrame); myFrame.getContentPane().removeAll(); myFrame.getContentPane().add(panel); myFrame.getContentPane().validate(); myFrame.getContentPane().repaint(); } }); } return jButton4; } /** * This method initializes jButton5 * * @return javax.swing.JButton */ private JButton getJButton5() { if (jButton5 == null) { jButton5 = new JButton(); jButton5.setLocation(new Point(506, 282)); jButton5.setIcon(new ImageIcon(getClass().getResource("/images/ui/images/quiz.jpg"))); jButton5.setSize(new Dimension(180, 105)); jButton5.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { JPanel panel = new QuizPanel(myFrame); myFrame.getContentPane().removeAll(); myFrame.getContentPane().add(panel); myFrame.getContentPane().validate(); myFrame.getContentPane().repaint(); } }); } return jButton5; } }
Java Code:Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at OOPJ.ui.MenuPanel.<init>(MenuPanel.java:53) at OOPJ.ui.UserProfile$1.actionPerformed(UserProfile.java:83) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$000(Unknown Source) at java.awt.EventQueue$1.run(Unknown Source) at java.awt.EventQueue$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$2.run(Unknown Source) at java.awt.EventQueue$2.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)
How come there is an nullpointerexception problem?
Please save me!Last edited by pbrockway2; 01-20-2012 at 05:56 AM. Reason: code tags added
- 01-20-2012, 05:59 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Re: NullPointerException Problems
Hi mathidioticz, welcome to the forums!
When you post code, use the "code" tags: put [code] at the start of the code and [/code] at the end. That way the code will be properly indented and readable when it appears here. Also it is a good idea to use spaces to indent code, especially when it is going to appear within a web browser as these can render the tabs quite wide.
- 01-20-2012, 06:09 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Re: NullPointerException Problems
You read these things from the top down until you hit a line of your code which is a good place to start looking for the cause of the problem. (Which you have correctly identified as line 53). The rest of the stack trace might be useful if you want to know what was going on just before the error occurred.Java Code:Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at OOPJ.ui.MenuPanel.<init>(MenuPanel.java:53) at OOPJ.ui.UserProfile$1.actionPerformed(UserProfile.java:83) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
A NullPointerException occurs when you have a variable or other expression that has the value null and you try and use it as if it had a non null value. Typically this is because you use the . operator to dereference something that is null, or you use the [] operator on an array which is null:
Line 53 says:Java Code:int[] arr; arr[42]; // bad! arr is null String foo; int len = foo.length(); // bad! foo is null
The two candidates for being null are jLabelHello and curUser. You can check which is null by using System.out.println()Java Code:jLabelHello.setText("Hello " + curUser.getUsername() );
Once you have found which is null go back through your code to where you thought the variable had been given a nonnull value and find out why that didn't happen.Java Code:System.out.println("About to set the name"); System.out.println("jLabelHello=" + jLabelHello); System.out.println("curUser=" + curUser); jLabelHello.setText("Hello " + curUser.getUsername() );
Similar Threads
-
InputStream/Jar Problems/File IO Problems
By rdjava in forum Advanced JavaReplies: 31Last Post: 01-17-2011, 11:12 AM -
NullPointerException
By GPB in forum New To JavaReplies: 8Last Post: 02-21-2010, 03:05 PM -
NullPointerException
By Juuno in forum New To JavaReplies: 1Last Post: 02-11-2010, 05:43 PM -
Why do I get a NullPointerException?
By nessa203 in forum New To JavaReplies: 2Last Post: 01-12-2010, 04:02 PM -
Why do I get a NullPointerException?
By nessa203 in forum New To JavaReplies: 5Last Post: 01-07-2010, 01:14 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks