Results 1 to 9 of 9
  1. #1
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Question NullPointerException error for null panel

    Hello all,

    Can anybody help me to solve the NullPointer exception error be reviewing the coding. I am getting it near by the line "pane.setLayout(null);" from the below snippet.


    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

    public class FruitJuiceMachine extends JFrame
    {
    private static final int WIDTH = 450;
    private static final int HEIGHT = 300;
    //Instance variables
    private CashRegister cashRegister = new CashRegister();
    private Dispenser appleJuice = new Dispenser(100, 80);
    private Dispenser orangeJuice = new Dispenser(100, 65);
    private Dispenser mangoLassi = new Dispenser(75, 75);
    private Dispenser fruitPunch = new Dispenser(100, 60);
    private JLabel headingMainL;
    private JLabel selectionL;
    private JLabel coinL;
    private JTextField coinInput;
    private JLabel showPrice;
    private JTextField dispenser;
    private JButton exitB, appleJuiceB, orangeJuiceB,
    mangoLassiB, fruitPunchB;
    private ButtonHandler pbHandler;

    public FruitJuiceMachine()
    {
    setTitle("Fruit Juice Machine");
    setSize(WIDTH, HEIGHT);
    Container pane = getContentPane();

    pane.setLayout(null);

    headingMainL.setSize(250, 30);
    headingMainL.setLocation(10, 10);
    selectionL.setSize(250, 30);
    selectionL.setLocation(10, 40);
    coinL.setSize(250, 30);
    coinL.setLocation(230, 100);
    coinL.setVisible(true);
    coinInput.setEditable(false);
    coinInput.setSize(200, 30);
    coinInput.setLocation(230, 130);
    showPrice.setSize(200, 30);
    showPrice.setLocation(230, 160);
    appleJuiceB.setSize(200, 30);
    appleJuiceB.setLocation(10, 70);
    orangeJuiceB.setSize(200, 30);
    orangeJuiceB.setLocation(10, 100);
    mangoLassiB.setSize(200, 30);
    mangoLassiB.setLocation(10, 130);
    fruitPunchB.setSize(200, 30);
    fruitPunchB.setLocation(10, 160);
    dispenser.setHorizontalAlignment(JTextField.CENTER );
    dispenser.setVisible(false);
    dispenser.setSize(350,30);
    dispenser.setLocation(40, 230);
    exitB.setSize(200, 30);
    exitB.setLocation(10, 190);

    pane.add(headingMainL);
    pane.add(selectionL);
    pane.add(coinL);
    pane.add(coinInput);
    pane.add(showPrice);
    pane.add(appleJuiceB);
    pane.add(orangeJuiceB);
    pane.add(mangoLassiB);
    pane.add(fruitPunchB);
    pane.add(dispenser);
    pane.add(exitB);


    pbHandler = new ButtonHandler(); //instantiate the object
    //register the listener with each button
    appleJuiceB.addActionListener(pbHandler);
    orangeJuiceB.addActionListener(pbHandler);
    mangoLassiB.addActionListener(pbHandler);
    fruitPunchB.addActionListener(pbHandler);
    exitB.addActionListener(pbHandler);


    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    } //end constructor
    // the inner class ButtonHandler here.
    private class ButtonHandler implements ActionListener
    {
    public void actionPerformed (ActionEvent e)
    {
    if (e.getActionCommand().equals("Exit"))
    System.exit(0);
    else
    {
    coinInput.setEditable(true);
    coinInput.setText("");
    dispenser.setVisible(true);
    dispenser.setText("");
    if (e.getActionCommand().equals("Apple Juice"))
    sellProduct(appleJuice, "Apple Juice");
    else if (e.getActionCommand().equals("Orange Juice"))
    sellProduct(orangeJuice, "Orange Juice");
    else if (e.getActionCommand().equals("Mango Lassi"))
    sellProduct(mangoLassi, " Mango Lassi");
    else if (e.getActionCommand().equals("Fruit Punch"))
    sellProduct(fruitPunch, "Fruit Punch");
    }
    }
    } // end ButtonHandler
    private void sellProduct(Dispenser product,
    String productName)
    {
    int coinsInserted = 0;
    int price;
    int coinsRequired;
    String str;
    if (product.getNumberOfItems() > 0)
    {
    price = product.getCost();
    coinsRequired = price - coinsInserted;
    showPrice.setText("Amount required: " + price);
    while (coinsRequired > 0)
    {
    str = JOptionPane.showInputDialog("Please insert "
    + coinsRequired + " cents");
    coinsInserted = coinsInserted
    + Integer.parseInt(str);

    coinInput.setText("" + coinsInserted);
    coinsRequired = price - coinsInserted;
    }
    cashRegister.acceptCash(coinsInserted);
    product.makeSale();
    dispenser.setText("Enjoy your " + productName
    + " and come again!");
    }
    else //dispenser is empty
    dispenser.setText("Sorry " + productName
    + " is sold out. Make another"
    + " selection!");
    }//end sellProduct
    public static void main(String[] args)
    {
    FruitJuiceMachine juiceShop = new FruitJuiceMachine();
    }
    }

    Please check and reply me soon.

    Regards,

    bhuvanakarthikk

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,656
    Blog Entries
    7
    Rep Power
    21

    Default

    That code is unreadable; please read this before you dump all your code here.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    Hello Jos,

    Please find the coding as below:

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class FruitJuiceMachine extends JFrame
    {
    private static final int WIDTH = 450;
    private static final int HEIGHT = 300;
    //Instance variables
    private CashRegister cashRegister = new CashRegister();
    private Dispenser appleJuice = new Dispenser(100, 80);
    private Dispenser orangeJuice = new Dispenser(100, 65);
    private Dispenser mangoLassi = new Dispenser(75, 75);
    private Dispenser fruitPunch = new Dispenser(100, 60);
    private JLabel headingMainL;
    private JLabel selectionL;
    private JLabel coinL;
    private JTextField coinInput;
    private JLabel showPrice;
    private JTextField dispenser;
    private JButton exitB, appleJuiceB, orangeJuiceB,
    mangoLassiB, fruitPunchB;
    private ButtonHandler pbHandler;
    
    public FruitJuiceMachine()
    {
    setTitle("Fruit Juice Machine");
    setSize(WIDTH, HEIGHT);
    Container pane = getContentPane();
    
    pane.setLayout(null);
    
    headingMainL.setSize(250, 30);
    headingMainL.setLocation(10, 10);
    selectionL.setSize(250, 30);
    selectionL.setLocation(10, 40);
    coinL.setSize(250, 30);
    coinL.setLocation(230, 100);
    coinL.setVisible(true);
    coinInput.setEditable(false);
    coinInput.setSize(200, 30);
    coinInput.setLocation(230, 130);
    showPrice.setSize(200, 30);
    showPrice.setLocation(230, 160);
    appleJuiceB.setSize(200, 30);
    appleJuiceB.setLocation(10, 70);
    orangeJuiceB.setSize(200, 30);
    orangeJuiceB.setLocation(10, 100);
    mangoLassiB.setSize(200, 30);
    mangoLassiB.setLocation(10, 130);
    fruitPunchB.setSize(200, 30);
    fruitPunchB.setLocation(10, 160);
    dispenser.setHorizontalAlignment(JTextField.CENTER);
    dispenser.setVisible(false);
    dispenser.setSize(350,30);
    dispenser.setLocation(40, 230);
    exitB.setSize(200, 30);
    exitB.setLocation(10, 190);
    
    pane.add(headingMainL);
    pane.add(selectionL);
    pane.add(coinL);
    pane.add(coinInput);
    pane.add(showPrice);
    pane.add(appleJuiceB);
    pane.add(orangeJuiceB);
    pane.add(mangoLassiB);
    pane.add(fruitPunchB);
    pane.add(dispenser);
    pane.add(exitB);
    
    
    pbHandler = new ButtonHandler(); //instantiate the object
    //register the listener with each button
    appleJuiceB.addActionListener(pbHandler);
    orangeJuiceB.addActionListener(pbHandler);
    mangoLassiB.addActionListener(pbHandler);
    fruitPunchB.addActionListener(pbHandler);
    exitB.addActionListener(pbHandler);
    
    
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    } //end constructor
    // the inner class ButtonHandler here.
    private class ButtonHandler implements ActionListener
    {
    public void actionPerformed (ActionEvent e)
    {
    if (e.getActionCommand().equals("Exit"))
    System.exit(0);
    else
    {
    coinInput.setEditable(true);
    coinInput.setText("");
    dispenser.setVisible(true);
    dispenser.setText("");
    if (e.getActionCommand().equals("Apple Juice"))
    sellProduct(appleJuice, "Apple Juice");
    else if (e.getActionCommand().equals("Orange Juice"))
    sellProduct(orangeJuice, "Orange Juice");
    else if (e.getActionCommand().equals("Mango Lassi"))
    sellProduct(mangoLassi, " Mango Lassi");
    else if (e.getActionCommand().equals("Fruit Punch"))
    sellProduct(fruitPunch, "Fruit Punch");
    }
    }
    } // end ButtonHandler
    private void sellProduct(Dispenser product,
    String productName)
    {
    int coinsInserted = 0;
    int price;
    int coinsRequired;
    String str;
    if (product.getNumberOfItems() > 0)
    {
    price = product.getCost();
    coinsRequired = price - coinsInserted;
    showPrice.setText("Amount required: " + price);
    while (coinsRequired > 0)
    {
    str = JOptionPane.showInputDialog("Please insert "
    + coinsRequired + " cents");
    coinsInserted = coinsInserted
    + Integer.parseInt(str);
    
    coinInput.setText("" + coinsInserted);
    coinsRequired = price - coinsInserted;
    }
    cashRegister.acceptCash(coinsInserted);
    product.makeSale();
    dispenser.setText("Enjoy your " + productName
    + " and come again!");
    }
    else //dispenser is empty
    dispenser.setText("Sorry " + productName
    + " is sold out. Make another"
    + " selection!");
    }//end sellProduct
    public static void main(String[] args)
    {
    FruitJuiceMachine juiceShop = new FruitJuiceMachine();
    }
    }
    I have also attached the CashRegister and Dispenser class coding in the attached text file.

    Best Regards,

    bhuvanakarthikk
    Attached Files Attached Files

  4. #4
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Hey Karthik,

    The problem in your code is that you are simply declaring the JLabel, JTextField and JButton, but you are not initializing them to anything. So, when you are trying to access them, the compiler is getting freaked out and giving you the NullPointerException.

    The problem is coming at this line,

    Java Code:
    headingMainL.setSize(250, 30);
    where you are trying to setSize for your JLabel which you haven't initialized.

    Initialize these things first and then operate on them. For e.g.

    Java Code:
    private JLabel headingMainL = new JLabel();
    Or something like this as per your need.

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,656
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by bhuvanakarthikk View Post
    Hello Jos,

    Please find the coding as below:
    Don't you indent your code? This is still unreadable. If you want us to help you, you have to help us; at least by showing us readable code. I bet you can hardly read your own code as it is now.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    Hi Goldest,

    Thanks for your reply. Its working after this update.

    Hi Jos,

    I have used [left] and [code] to enclose the text and program coding respectively. Still, I am adding the code with \[indent\] now. Please check whether you are able to view properly.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class FruitJuiceMachine extends JFrame
    {
    [indent]private static final int WIDTH = 450;[/indent]
    [indent]private static final int HEIGHT = 300;[/indent]
    [indent]//Instance variables[/indent]
    [indent]private CashRegister cashRegister = new CashRegister();[/indent]
    [indent]private Dispenser appleJuice = new Dispenser(100, 80);[/indent]
    [indent]private Dispenser orangeJuice = new Dispenser(100, 65);[/indent]
    [indent]private Dispenser mangoLassi = new Dispenser(75, 75);[/indent]
    [indent]private Dispenser fruitPunch = new Dispenser(100, 60);[/indent]
    [indent]private JLabel headingMainL;[/indent]
    [indent]private JLabel selectionL;[/indent]
    [indent]private JLabel coinL;[/indent]
    [indent]private JTextField coinInput;[/indent]
    [indent]private JLabel showPrice;[/indent]
    [indent]private JTextField dispenser;[/indent]
    [indent]private JButton exitB, appleJuiceB, orangeJuiceB,[/indent]
    [indent]mangoLassiB, fruitPunchB;[/indent]
    [indent]private ButtonHandler pbHandler;[/indent]
    
    public FruitJuiceMachine()
    {
    [indent]setTitle("Fruit Juice Machine");[/indent]
    [indent]setSize(WIDTH, HEIGHT);[/indent]
    [indent]Container pane = getContentPane();[/indent]
    
    [indent]pane.setLayout(null);[/indent]
    
    [indent]headingMainL.setSize(250, 30);[/indent]
    [indent]headingMainL.setLocation(10, 10);[/indent]
    [indent]selectionL.setSize(250, 30);[/indent]
    [indent]selectionL.setLocation(10, 40);[/indent]
    [indent]coinL.setSize(250, 30);[/indent]
    [indent]coinL.setLocation(230, 100);[/indent]
    [indent]coinL.setVisible(true);[/indent]
    [indent]coinInput.setEditable(false);[/indent]
    [indent]coinInput.setSize(200, 30);[/indent]
    [indent]coinInput.setLocation(230, 130);[/indent]
    [indent]showPrice.setSize(200, 30);[/indent]
    [indent]showPrice.setLocation(230, 160);[/indent]
    [indent]appleJuiceB.setSize(200, 30);[/indent]
    [indent]appleJuiceB.setLocation(10, 70);[/indent]
    [indent]orangeJuiceB.setSize(200, 30);[/indent]
    [indent]orangeJuiceB.setLocation(10, 100);[/indent]
    [indent]mangoLassiB.setSize(200, 30);[/indent]
    [indent]mangoLassiB.setLocation(10, 130);[/indent]
    [indent]fruitPunchB.setSize(200, 30);[/indent]
    [indent]fruitPunchB.setLocation(10, 160);[/indent]
    [indent]dispenser.setHorizontalAlignment(JTextField.CENTER);[/indent]
    [indent]dispenser.setVisible(false);[/indent]
    [indent]dispenser.setSize(350,30);[/indent]
    [indent]dispenser.setLocation(40, 230);[/indent]
    [indent]exitB.setSize(200, 30);[/indent]
    [indent]exitB.setLocation(10, 190);[/indent]
    
    [indent]pane.add(headingMainL);[/indent]
    [indent]pane.add(selectionL);[/indent]
    [indent]pane.add(coinL);[/indent]
    [indent]pane.add(coinInput);[/indent]
    [indent]pane.add(showPrice);[/indent]
    [indent]pane.add(appleJuiceB);[/indent]
    [indent]pane.add(orangeJuiceB);[/indent]
    [indent]pane.add(mangoLassiB);[/indent]
    [indent]pane.add(fruitPunchB);[/indent]
    [indent]pane.add(dispenser);[/indent]
    [indent]pane.add(exitB);[/indent]
    
    
    [indent]pbHandler = new ButtonHandler(); //instantiate the object[/indent]
    [indent]//register the listener with each button[/indent]
    [indent]appleJuiceB.addActionListener(pbHandler);[/indent]
    [indent]orangeJuiceB.addActionListener(pbHandler);[/indent]
    [indent]mangoLassiB.addActionListener(pbHandler);[/indent]
    [indent]fruitPunchB.addActionListener(pbHandler);[/indent]
    [indent]exitB.addActionListener(pbHandler);[/indent]
    
    
    [indent]setVisible(true);[/indent]
    [indent]setDefaultCloseOperation(EXIT_ON_CLOSE);[/indent]
    } //end constructor
    // the inner class ButtonHandler here.
    private class ButtonHandler implements ActionListener
    {
    [indent]public void actionPerformed (ActionEvent e)[/indent]
    [indent]{[/indent]
    [indent][indent]if (e.getActionCommand().equals("Exit"))[/indent][/indent]
    [indent][indent]System.exit(0);[/indent][/indent]
    [indent][indent]else[/indent][/indent]
    [indent][indent]{[/indent][/indent]
    [indent][indent]coinInput.setEditable(true);[/indent][/indent]
    [indent][indent]coinInput.setText("");[/indent][/indent]
    [indent][indent]dispenser.setVisible(true);[/indent][/indent]
    [indent][indent]dispenser.setText("");[/indent][/indent]
    [indent][indent]if (e.getActionCommand().equals("Apple Juice"))[/indent][/indent]
    [indent][indent]sellProduct(appleJuice, "Apple Juice");[/indent][/indent]
    [indent][indent]else if (e.getActionCommand().equals("Orange Juice"))[/indent][/indent]
    [indent][indent]sellProduct(orangeJuice, "Orange Juice");[/indent][/indent]
    [indent][indent]else if (e.getActionCommand().equals("Mango Lassi"))[/indent][/indent]
    [indent][indent]sellProduct(mangoLassi, " Mango Lassi");[/indent][/indent]
    [indent][indent]else if (e.getActionCommand().equals("Fruit Punch"))[/indent][/indent]
    [indent][indent]sellProduct(fruitPunch, "Fruit Punch");[/indent][/indent]
    [indent][indent]}[/indent][/indent]
    [indent][indent]}[/indent][/indent]
    } // end ButtonHandler
    private void sellProduct(Dispenser product, String productName)
    {
    [indent]int coinsInserted = 0;[/indent]
    [indent]int price;[/indent]
    [indent]int coinsRequired;[/indent]
    [indent]String str;[/indent]
    [indent]if (product.getNumberOfItems() > 0)[/indent]
    [indent]{[/indent]
    [indent]price = product.getCost();[/indent]
    [indent]coinsRequired = price - coinsInserted;[/indent]
    [indent]showPrice.setText("Amount required: " + price);[/indent]
    [indent]while (coinsRequired > 0)[/indent]
    [indent]{[/indent]
    [indent]str = JOptionPane.showInputDialog("Please insert " + coinsRequired + " cents");[/indent]
    [indent]coinsInserted = coinsInserted + Integer.parseInt(str);[/indent]
    
    [indent]coinInput.setText("" + coinsInserted);[/indent]
    [indent]coinsRequired = price - coinsInserted;[/indent]
    [indent]}[/indent]
    [indent]cashRegister.acceptCash(coinsInserted);[/indent]
    [indent]product.makeSale();[/indent]
    [indent]dispenser.setText("Enjoy your " + productName + " and come again!");[/indent]
    [indent]}[/indent]
    [indent]else //dispenser is empty[/indent]
    [indent]dispenser.setText("Sorry " + productName + " is sold out. Make another" + " selection!"); }//end sellProduct[/indent]
    public static void main(String[] args)[/indent]
    {
    [indent]FruitJuiceMachine juiceShop = new FruitJuiceMachine();[/indent]
    }
    }
    Best Regards to both,

    bhuvana karthikk

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,656
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by bhuvanakarthikk View Post
    Hi Jos,

    I have used [left] and [code] to enclose the text and program coding respectively. Still, I am adding the code with \[indent\] now. Please check whether you are able to view properly.
    No, no, no, don't do it that way; before you paste your code in the edit window type [code] (including the square brackets and no spaces); past your code and below it type the tag [/code] (also with square brackets and no spaces). So this:

    [code]
    public static class Test {
    public static void main() {
    System.out.println("Hello world!");
    }
    }
    [/code]

    shows up like this:

    Java Code:
    public static class Test {
       public static void main() {
          System.out.println("Hello world!");
       }
    }
    Note that below the edit window is a 'Preview Post' button. If you press it you can check how your post shows up so you can edit it if needed.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Or if you are using IDE like eclipse, you can do the formatting through the tool.

    Use Ctrl + A -> To select your entire java code.

    And then do,

    Ctrl + Shift + F -> To format your code.

    This will indent your code in a standard way. Later, you can post that code here right away within the code tags.

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  9. #9
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

Similar Threads

  1. Need Help with NullPointerException Error
    By waterisgood5 in forum New To Java
    Replies: 2
    Last Post: 11-10-2010, 07:27 PM
  2. NullPointerException error
    By Aggror in forum New To Java
    Replies: 2
    Last Post: 09-29-2010, 02:31 PM
  3. Replies: 1
    Last Post: 12-05-2009, 07:28 AM
  4. Null Error
    By scoleman123 in forum New To Java
    Replies: 2
    Last Post: 09-19-2008, 04:04 PM
  5. ERROR: nullPointerException
    By mathias in forum New To Java
    Replies: 1
    Last Post: 08-05-2007, 06:54 AM

Posting Permissions

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