#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    3
    Rep Power
    0

    Passing value and setting it to jlabel text


    This site is really helping a lot of people! I have a simple program that I can't understand why I am getting the NullPointerException each time I try to pass a value from one form to another and setting it up as a text in a label. If I pass the value and do the pritln, I can actually see the result but if I place it in the jlabel as text, that's the time I get the error. Please Help! Thanks! Here is the code to pass the value to the other form.

    Code:
        public class Micolocoi extends JApplet {  
        private String lblName;  
          
        public JLabel lblTime, lblRem1, lblRem2, lblRem3, lblRem4, lblRem5;  
        lblRem1 = new JLabel("0", SwingConstants.CENTER);  
        lblRem2 = new JLabel("0", SwingConstants.CENTER);  
        lblRem3 = new JLabel("0", SwingConstants.CENTER);  
        lblRem4 = new JLabel("0", SwingConstants.CENTER);  
        lblRem5 = new JLabel("0", SwingConstants.CENTER);  
          
        public void setRem1(String input) { lblName = input; System.out.println(input); } //TO GET THE VALUE FROM THE OTHER FORM  
        public String getRem1() { return lblName; }  
        public void setLblRem1(){ lblRem1.setText(getRem1()); } //TO SET UP THE VALUE AS THE TEXT OF THE LABEL  
        }  
        public static void main(String args[]){  
          
        Micolocoi micolocoi = new Micolocoi();  
        JFrame frame = new JFrame("MICOLOCOI");  
        Container container = frame.getContentPane();  
        container.add(micolocoi);  
        frame.setSize(600, 200);  
        frame.setResizable(false);  
        frame.setLocationRelativeTo(null);  
          
          
        micolocoi.init();  
        micolocoi.start();  
        micolocoi.getDateTime();  
          
        frame.setVisible(true);  
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);  
          
        }  
        }  
          
          
        public class Choice extends JFrame {  
          
        btnPrepaid.addActionListener(new ActionListener() {  
          
        public void actionPerformed(ActionEvent e) {  
        selectionAction(e);  
        }  
        });  
          
        private void selectionAction(ActionEvent evt){  
        System.out.println(choice);  
        int choice = getChoice();  
        boolean bol = true;  
        switch (choice) {  
          
        case 1:  
        while(bol){  
        String str = JOptionPane.showInputDialog(null, "Input minutes ", "ALERT", 1);  
        if(str != null && Integer.parseInt(str) != 0){  
        JOptionPane.showMessageDialog(null, "Time will run for " +str +" minutes", "ALERT", 1);  
        mic.setRem1(str); //THIS IS WHAT I AM TRYING TO PASS TO THE OTHER FORM AND SET IT AS THE TEXT FOR THE LABEL SO I CAN ACTUALLY SUBTRACT 1 EACH MINUTE  
        mic.setLblRem1();  
        setVisible(false);  
        bol = false;  
        } else {  
        JOptionPane.showMessageDialog(null, "No input given", "ALERT", 1);  
        group.clearSelection();  
        }  
        }  
        break;  
          
        case 2:  
        while(bol){  
        String str = JOptionPane.showInputDialog(null, "Input minutes ", "ALERT", 1);  
          
        if(str != null && Integer.parseInt(str) != 0){  
        JOptionPane.showMessageDialog(null, "Time will run for " +str +" minutes", "ALERT", 1);  
        System.out.println("dfsadsf");  
        setVisible(false);  
        bol = false;  
        break;  
        } else {  
        JOptionPane.showMessageDialog(null, "No input given", "ALERT", 1);  
        group.clearSelection();  
        }  
        }  
        break;  
        }....
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    I am getting the NullPointerException
    Please post the full text of the error message.
    Look at the line in your code where the exception is happening, find the variable with the null value and then backtrack in the code to see why that variable does not have a valid non-null value.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    3
    Rep Power
    0
    Originally Posted by NormR
    Please post the full text of the error message.
    Look at the line in your code where the exception is happening, find the variable with the null value and then backtrack in the code to see why that variable does not have a valid non-null value.
    This is my whole code sir..
    [CODE = java] import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.GregorianCalendar;


    import javax.swing.JApplet;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JTextField;
    import javax.swing.SwingConstants;
    import javax.swing.Timer;


    public class Micolocoi extends JApplet {

    private JLabel lblComputer, lblTimeIn, lblTimeOut, lblRemainingTIme, lblDate, lblAmount,lblPC1, lblPC2, lblPC3, lblPC4, lblPC5;
    private JTextField txtIn1, txtIn2, txtIn3, txtIn4, txtIn5, txtOut1, txtOut2, txtOut3, txtOut4, txtOut5,
    txtAmount1, txtAmount2, txtAmount3, txtAmount4, txtAmount5;
    private JButton btn1, btn2, btn3, btn4, btn5;
    private String lblName;

    public JLabel lblTime, lblRem1, lblRem2, lblRem3, lblRem4, lblRem5;
    public int time;

    Calendar calendar = new GregorianCalendar();
    int month = calendar.get(Calendar.MONTH);
    int day = calendar.get(Calendar.DAY_OF_MONTH);
    int year = calendar.get(Calendar.YEAR);

    SimpleDateFormat ft = new SimpleDateFormat ("hh:mm:ss a"); //("dd/MM/yyyy 'at' hh:mm:ss a");
    Date dNow = new Date( );
    // -------------------------------------------------------------------------------------------------------------------------
    Timer timer = new Timer(1000, new ActionListener() {

    public void actionPerformed(ActionEvent e) {
    lblTime.setText(getDateTime());
    }
    });
    // -------------------------------------------------------------------------------------------------------------------------

    public String getDateTime() {
    DateFormat dateFormat = new SimpleDateFormat("hh:mm:ss aa");
    Date date = new Date();
    timer.start();
    return dateFormat.format(date);
    }
    // -------------------------------------------------------------------------------------------------------------------------

    public void init(){

    lblComputer = new JLabel("Computer", SwingConstants.CENTER);
    lblTime = new JLabel();
    lblTimeIn = new JLabel("Time In", SwingConstants.CENTER);
    lblTimeOut = new JLabel("Time Out", SwingConstants.CENTER);
    lblRemainingTIme = new JLabel("Remaining", SwingConstants.CENTER);
    lblAmount = new JLabel("Tender", SwingConstants.CENTER);
    lblPC1 = new JLabel("Computer 1", SwingConstants.CENTER);
    lblPC2 = new JLabel("Computer 2", SwingConstants.CENTER);
    lblPC3 = new JLabel("Computer 3", SwingConstants.CENTER);
    lblPC4 = new JLabel("Computer 4", SwingConstants.CENTER);
    lblPC5 = new JLabel("Computer 5", SwingConstants.CENTER);
    lblRem1 = new JLabel("0", SwingConstants.CENTER);
    lblRem2 = new JLabel("0", SwingConstants.CENTER);
    lblRem3 = new JLabel("0", SwingConstants.CENTER);
    lblRem4 = new JLabel("0", SwingConstants.CENTER);
    lblRem5 = new JLabel("0", SwingConstants.CENTER);
    txtIn1 = new JTextField(10);
    txtIn2 = new JTextField(10);
    txtIn3 = new JTextField(10);
    txtIn4 = new JTextField(10);
    txtIn5 = new JTextField(10);
    txtOut1 = new JTextField(10);
    txtOut2 = new JTextField(10);
    txtOut3 = new JTextField(10);
    txtOut4 = new JTextField(10);
    txtOut5 = new JTextField(10);
    txtAmount1 = new JTextField(10);
    txtAmount2 = new JTextField(10);
    txtAmount3 = new JTextField(10);
    txtAmount4 = new JTextField(10);
    txtAmount5 = new JTextField(10);
    btn1 = new JButton("START/STOP");
    btn1.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent e) {
    computerActions(e);

    }
    });
    btn2 = new JButton("START/STOP");
    btn2.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent e) {
    computerActions(e);

    }
    });
    btn3 = new JButton("START/STOP");
    btn4 = new JButton("START/STOP");
    btn5 = new JButton("START/STOP");

    Container container = (super.getContentPane());
    container.setLayout(new GridLayout(9,8));

    container.add(new JLabel(""));
    container.add(new JLabel(""));
    container.add(new JLabel(""));
    container.add(new JLabel(""));
    container.add(new JLabel(""));
    container.add(new JLabel(month +"/" +day +"/" +year));
    container.add(lblComputer);
    container.add(lblTimeIn);
    container.add(lblTimeOut);
    container.add(lblRemainingTIme);
    container.add(lblAmount);
    container.add(new JLabel("Start/Stop"));
    container.add(new JLabel(""));
    container.add(new JLabel(""));
    container.add(new JLabel(""));
    container.add(new JLabel(""));
    container.add(new JLabel(""));
    container.add(lblTime);
    container.add(lblPC1);
    container.add(txtIn1);
    container.add(txtOut1);
    container.add(lblRem1);
    container.add(txtAmount1);
    container.add(btn1);
    container.add(lblPC2);
    container.add(txtIn2);
    container.add(txtOut2);
    container.add(lblRem2);
    container.add(txtAmount2);
    container.add(btn2);
    container.add(lblPC3);
    container.add(txtIn3);
    container.add(txtOut3);
    container.add(lblRem3);
    container.add(txtAmount3);
    container.add(btn3);
    container.add(lblPC4);
    container.add(txtIn4);
    container.add(txtOut4);
    container.add(lblRem4);
    container.add(txtAmount4);
    container.add(btn4);
    container.add(lblPC5);
    container.add(txtIn5);
    container.add(txtOut5);
    container.add(lblRem5);
    container.add(txtAmount5);
    container.add(btn5);

    }
    // -------------------------------------------------------------------------------------------------------------------------

    public void computerActions(ActionEvent evt){

    if(evt.getSource() == btn1){
    txtIn1.setText(lblTime.getText());
    new Choice(1);
    } else if(evt.getSource() == btn2){
    txtIn2.setText(lblTime.getText());
    new Choice(2);
    }
    }
    // -------------------------------------------------------------------------------------------------------------------------

    public void setRem1(String input) { lblName = input; System.out.println(input); }
    public String getRem1() { return lblName; }
    public void setLblRem1(){ lblRem1.setText(getRem1()); }
    // -------------------------------------------------------------------------------------------------------------------------

    public static void main(String args[]){

    Micolocoi micolocoi = new Micolocoi();
    JFrame frame = new JFrame("MICOLOCOI");
    Container container = frame.getContentPane();
    container.add(micolocoi);
    frame.setSize(600, 200);
    frame.setResizable(false);
    frame.setLocationRelativeTo(null);


    micolocoi.init();
    micolocoi.start();
    micolocoi.getDateTime();

    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
    }

    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.ButtonGroup;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JRadioButton;


    public class Choice extends JFrame {
    private JRadioButton btnPrepaid, btnPostpaid;
    private ButtonGroup group;
    private Toolkit toolkit; //frame appears at the center of the screen
    private Dimension dimension; // frame appears at the center of the screen

    private static int choice;
    final Micolocoi mic = new Micolocoi();

    public Choice(final int inChoice){

    super("RENT TYPE");
    super.setSize(400, 100);
    super.setResizable(false);


    group = new ButtonGroup();

    setChoice(inChoice);

    btnPrepaid = new JRadioButton("Pre-paid");
    btnPrepaid.addActionListener(new ActionListener() {
    // -------------------------------------------------------------------------------------------------------
    public void actionPerformed(ActionEvent e) {
    selectionAction(e);
    }
    });
    btnPostpaid = new JRadioButton("Post-paid");

    group.add(btnPrepaid);
    group.add(btnPostpaid);

    Container container = super.getContentPane();
    container.setLayout(new GridLayout(2,3));

    container.add(new JLabel("Select Rent Type"));
    container.add(new JLabel(""));
    container.add(new JLabel(""));
    container.add(new JLabel(""));
    container.add(new JLabel(""));
    container.add(btnPrepaid);
    container.add(btnPostpaid);

    super.setVisible(true);
    super.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    toolkit = getToolkit();
    dimension = toolkit.getScreenSize();
    // to center frame
    setLocationRelativeTo(null);
    }
    // -------------------------------------------------------------------------------------------------------

    public void setChoice(int inChoice) { choice = inChoice; }
    public int getChoice() { return choice; }
    // -------------------------------------------------------------------------------------------------------

    private void selectionAction(ActionEvent evt){
    System.out.println(choice);
    int choice = getChoice();
    boolean bol = true;
    switch (choice) {

    case 1:
    while(bol){
    String str = JOptionPane.showInputDialog(null, "Input minutes ", "ALERT", 1);
    if(str != null && Integer.parseInt(str) != 0){
    JOptionPane.showMessageDialog(null, "Time will run for " +str +" minutes", "ALERT", 1);
    mic.setRem1(str);
    mic.setLblRem1();
    setVisible(false);
    bol = false;
    } else {
    JOptionPane.showMessageDialog(null, "No input given", "ALERT", 1);
    group.clearSelection();
    }
    }
    break;

    case 2:
    while(bol){
    String str = JOptionPane.showInputDialog(null, "Input minutes ", "ALERT", 1);

    if(str != null && Integer.parseInt(str) != 0){
    JOptionPane.showMessageDialog(null, "Time will run for " +str +" minutes", "ALERT", 1);
    System.out.println("dfsadsf");
    setVisible(false);
    bol = false;
    break;
    } else {
    JOptionPane.showMessageDialog(null, "No input given", "ALERT", 1);
    group.clearSelection();
    }
    }
    break;
    }
    }

    }

    [/CODE]

    This is the stack trace sir..

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Micolocoi.setLblRem1(Micolocoi.java:181)
    at Choice.selectionAction(Choice.java:83)
    at Choice.access$0(Choice.java:71)
    at Choice$1.actionPerformed(Choice.java:39)
    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.JToggleButton$ToggleButtonModel.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$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$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)
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Please edit your post and fix the code tags. They are not working correctly.
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Micolocoi.setLblRem1(Micolocoi.java:181)
    There is a variable at line 181 with a null value causing the problem. Look at line 181, find the variable with the null value and then backtrack in the code to find out why that variable does not have a valid non-null value.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    3
    Rep Power
    0
    Originally Posted by NormR
    Please edit your post and fix the code tags. They are not working correctly.

    There is a variable at line 181 with a null value causing the problem. Look at line 181, find the variable with the null value and then backtrack in the code to find out why that variable does not have a valid non-null value.
    Yes sir. I know. I traced that before I posted this program. What I can't figure out is, why is it that I can actually "println" the value. So that means, it is not a null value. But when I actually try to place it in the label, I get this error. I'm really puzzled!
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Can you post the code around line 181 with the println statement just before line 181 that shows the values of all the variables used on line 181?
    Also post the output from the program that shows what was printed out and the error message.

IMN logo majestic logo threadwatch logo seochat tools logo