Results 1 to 9 of 9
- 08-08-2013, 11:41 PM #1
Member
- Join Date
- Aug 2013
- Posts
- 5
- Rep Power
- 0
Why do i get a null pointer exception
this is the error:
Exception in thread "main" java.lang.NullPointerException
at view.InfoPanel.<init>(InfoPanel.java:32)
at controller.Controller.makeFrame(Controller.java:41 )
at controller.Controller.<init>(Controller.java:25)
at controller.Main.main(Main.java:11)
this is the code:
package controller;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import javax.swing.*;
import view.ControlPanel;
import view.InfoPanel;
//import view.ProgramView;
import view.BoardView;
//import model.Program;
public class Controller extends JFrame implements ActionListener
{
private ControlPanel controlpanel;
private InfoPanel infopanel;
private BoardView boardview;
public Controller()
{
super("Chess Manager");
makeFrame();
makeMenuBar(this);
this.pack();
}
private void makeFrame(){
setSize(650, 650);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container contentpane = super.getContentPane();
contentpane.setLayout(new BorderLayout());
contentpane.setBackground(new Color(204,229,255));
controlpanel = new ControlPanel(this);
contentpane.add(controlpanel, BorderLayout.EAST);
infopanel = new InfoPanel(this);
contentpane.add(infopanel, BorderLayout.SOUTH);
boardview = new BoardView(this);
contentpane.add(boardview, BorderLayout.WEST);
}
private void makeMenuBar(Controller controller){
JMenuBar menubar = new JMenuBar();
super.setJMenuBar(menubar);
JMenu fileMenu = new JMenu("File");
menubar.add(fileMenu);
JMenu editMenu = new JMenu("Edit");
menubar.add(editMenu);
JMenu optionMenu = new JMenu("Options");
menubar.add(optionMenu);
JMenu extraMenu = new JMenu("Extra");
menubar.add(extraMenu);
}
private void setupProgram(){
}
public void actionPerformed(ActionEvent e){
}
}
__________________________________________________ __________________________________________________ _______
package view;
import java.awt.*;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.BorderFactory;
import controller.Controller;
import model.Subscription;
public class InfoPanel extends JPanel
{
private JTextField subscriptionbar;
private JTextPane fenbar;
private JTextArea infobar;
private Subscription subscription;
public InfoPanel (Controller controller){
setLayout(new GridBagLayout());
setBackground(new Color(204,229,255));
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(10,0,10,0);
subscriptionbar = new JTextField(subscription.getSubscription());
gbc.gridy = 0;
subscriptionbar .setPreferredSize(new Dimension(500, 25));
subscriptionbar .setBorder(BorderFactory.createLineBorder(Color.BL ACK));
add(subscriptionbar, gbc);
fenbar = new JTextPane();
gbc.gridy = 1;
fenbar .setPreferredSize(new Dimension(500, 25));
fenbar .setBorder(BorderFactory.createLineBorder(Color.BL ACK));
add(fenbar, gbc);
infobar = new JTextArea();
gbc.gridy = 2;
infobar .setPreferredSize(new Dimension(500, 150));
infobar .setBorder(BorderFactory.createLineBorder(Color.BL ACK));
infobar .setEditable(false);
add(infobar, gbc);
}
}
- 08-08-2013, 11:56 PM #2
Member
- Join Date
- Aug 2013
- Posts
- 5
- Rep Power
- 0
Re: Why do i get a null pointer exception
How can i fix this?
- 08-09-2013, 12:13 AM #3
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: Why do i get a null pointer exception
Try actually reading the error you are getting - its there to help you, not to hinder you.
It is telling you all the information you need: on line 32 of your InfoPanel class, an object instance variable contains a null-value while it should actually be pointing to a valid object; so somewhere you are forgetting to initialize a variable or class member. With that information in your pocket, go find out what is null on that line and where things go wrong. Judging from the code you posted, it can be only one thing. I'm not going to tell you what, I would rob you of a learning experience.
BTW, you should read this. It will help you get better help faster in the future:
http://www.java-forums.org/forum-gui...w-members.html"Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
- 08-09-2013, 12:34 AM #4
Member
- Join Date
- Aug 2013
- Posts
- 5
- Rep Power
- 0
Re: Why do i get a null pointer exception
I learn by doing and listening, i really need information! and i read "Guide For New Members" next time i will use this standard
Last edited by SpockVulcan; 08-09-2013 at 12:38 AM.
- 08-09-2013, 12:39 AM #5
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: Why do i get a null pointer exception
Nothing wrong with that at all, but please add reading to your list of activities, or learning programming is going to be a gruesome and tiresome experience for you:
Lesson: Exceptions (The Java™ Tutorials > Essential Classes)"Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
- 08-09-2013, 12:49 AM #6
Member
- Join Date
- Aug 2013
- Posts
- 5
- Rep Power
- 0
Re: Why do i get a null pointer exception
I dont want to nag especially because i am new..but exceptions doesnt solve the problem right? it only catches the error and you can output for instance a string
- 08-09-2013, 12:59 AM #7
Member
- Join Date
- Aug 2013
- Posts
- 5
- Rep Power
- 0
Re: Why do i get a null pointer exception
i need that infopanel object in my controller class for the JFrame
Java Code:public class Controller extends JFrame implements ActionListener
- 08-09-2013, 10:53 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Re: Why do i get a null pointer exception
gimbal2 has said exactly where your error is.
Indeed the exception says it as well.
Line 32 of InfoPanel.
So look at line 32 (I have no idea which one that is as your code is not formatted and you haven't highlighted which one it is).
What on that line have you failed to initialise?Please do not ask for code as refusal often offends.
** This space for rent **
- 08-09-2013, 11:19 AM #9
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: Why do i get a null pointer exception
No you need to solve the problem. That is true now and will always be true, tools and code are not going to do the work for you.
But to solve the problem you must first understand the problem and reading is your only solution there. What exactly is a NullPointerException? You'll be seeing a lot of them, so its a good idea to find out."Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
Similar Threads
-
Null pointer exception a
By TaxpayersMoney in forum New To JavaReplies: 5Last Post: 08-16-2011, 01:37 AM -
Null pointer exception
By samuel.roshni in forum Java ServletReplies: 14Last Post: 01-22-2011, 03:25 PM -
Null pointer exception?
By coffee in forum New To JavaReplies: 4Last Post: 08-03-2009, 04:22 AM -
Null Pointer Exception
By Jacinth in forum New To JavaReplies: 4Last Post: 01-22-2009, 02:47 PM -
Null Pointer Exception
By demiser55 in forum New To JavaReplies: 1Last Post: 09-22-2008, 07:33 PM
Bookmarks