Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Null Pointer Exception can't figure out how to solve this

  1. #1
    Member
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    38
    My Mood
    Inspired
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Null Pointer Exception can't figure out how to solve this

    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)

    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
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,237
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Null Pointer Exception can't figure out how to solve this

    Something is null on line 32 of the InfoPanel class...what variables are on that line? Is there a variable on that line that may not have been instantiated? (hint: is there an instance variable in the InfoPanel class that is never constructed?)

  3. #3
    Member
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    38
    My Mood
    Inspired
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Null Pointer Exception can't figure out how to solve this

    subscriptionbar = new JTextField(subscription.getSubscription());

    is on that line"32/33"

    the compiler works but when i run the main class which makes a new Controller object
     new Controller().setVisible(true)

    i get the following 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)

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,237
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Null Pointer Exception can't figure out how to solve this

    is on that line"32/33"
    A NullPointerException is thrown when something is null - or has not been instantiated (ether assigned or a new instance created). So there must be a variable on that line which was never assigned to a new instance (hint - do you ever assign the variable subscription to anything?).

  5. #5
    Member
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    38
    My Mood
    Inspired
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Null Pointer Exception can't figure out how to solve this

    yes, subscription is a variable which contains a Subcription object. This class is in a different package named model

    import model.Subscription;
     
    private Subscription subscription;

    I can post the code of that class as well if that helps

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,237
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Null Pointer Exception can't figure out how to solve this


  7. #7
    Member
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    38
    My Mood
    Inspired
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Null Pointer Exception can't figure out how to solve this

    Main Class package controller

    package controller;
     
    import controller.Controller;
     
    public class Main {
     
        public static void main (String[] args){
               new Controller().setVisible(true);
        }
    }

    Subscription Class package model
    package model;
     
     
    /**
     * Write a description of class Fen here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    public class Subscription
    {
     
        private final String SUBSCRIPTIONBAR = "Default Position";
     
        public Subscription(){
     
        }
     
        public String getSubscription(){
            return SUBSCRIPTIONBAR;
        }
     
     
    }

  8. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,237
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Null Pointer Exception can't figure out how to solve this

    I still don't see where the variable is instantiated.

  9. #9
    Member
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    38
    My Mood
    Inspired
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Null Pointer Exception can't figure out how to solve this

    yes thanks for your help i did not initiate a variable. Now i want a anonymous object

    --- Update ---

    Great site by the way!! I think all post more often these days

  10. #10
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,470
    My Mood
    Grumpy
    Thanks
    0
    Thanked 159 Times in 150 Posts

    Default Re: Null Pointer Exception can't figure out how to solve this

    Improving the world one idiot at a time!

  11. The Following User Says Thank You to Junky For This Useful Post:

    copeg (August 9th, 2013)

  12. #11
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,237
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Null Pointer Exception can't figure out how to solve this

    @gokuball, please read
    The problems with cross-posting

Similar Threads

  1. help me out from null pointer exception!!!
    By rishi in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 26th, 2013, 09:11 AM
  2. null pointer exception
    By Ramzi89 in forum Object Oriented Programming
    Replies: 1
    Last Post: August 15th, 2012, 01:57 PM
  3. Can't Figure out Why I am Getting Null Pointer Exception
    By seizitp in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 11th, 2012, 10:31 AM
  4. Help with null pointer exception
    By Dr.HughMan in forum Exceptions
    Replies: 35
    Last Post: November 30th, 2011, 08:00 PM
  5. Null Pointer Exception
    By MysticDeath in forum Exceptions
    Replies: 2
    Last Post: October 24th, 2009, 01:49 PM