Results 1 to 9 of 9
  1. #1
    SpockVulcan is offline Member
    Join Date
    Aug 2013
    Posts
    5
    Rep Power
    0

    Default 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);
    }

    }

  2. #2
    SpockVulcan is offline Member
    Join Date
    Aug 2013
    Posts
    5
    Rep Power
    0

    Default Re: Why do i get a null pointer exception

    How can i fix this?

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,914
    Rep Power
    5

    Default 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:

    Guide For New Members
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    SpockVulcan is offline Member
    Join Date
    Aug 2013
    Posts
    5
    Rep Power
    0

    Default 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-08-2013 at 11:38 PM.

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,914
    Rep Power
    5

    Default 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

  6. #6
    SpockVulcan is offline Member
    Join Date
    Aug 2013
    Posts
    5
    Rep Power
    0

    Default 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

  7. #7
    SpockVulcan is offline Member
    Join Date
    Aug 2013
    Posts
    5
    Rep Power
    0

    Default 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

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,967
    Rep Power
    19

    Default 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 **

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,914
    Rep Power
    5

    Default Re: Why do i get a null pointer exception

    Quote Originally Posted by SpockVulcan View Post
    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
    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

  1. Null pointer exception a
    By TaxpayersMoney in forum New To Java
    Replies: 5
    Last Post: 08-16-2011, 12:37 AM
  2. Null pointer exception
    By samuel.roshni in forum Java Servlet
    Replies: 14
    Last Post: 01-22-2011, 02:25 PM
  3. Null pointer exception?
    By coffee in forum New To Java
    Replies: 4
    Last Post: 08-03-2009, 03:22 AM
  4. Null Pointer Exception
    By Jacinth in forum New To Java
    Replies: 4
    Last Post: 01-22-2009, 01:47 PM
  5. Null Pointer Exception
    By demiser55 in forum New To Java
    Replies: 1
    Last Post: 09-22-2008, 06:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •