0

When I try to dynamically update my jlabel1 with 'j1.setText("");' it doesn't work and causes it to spam out a ton of errors, any insight to a solution to this issue would be greatly appreciated. For testing purposes enter the sin: 130692544.

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


class SinChecker extends JFrame
{ //naming variables
JTextField t1;
static JLabel j, j1, j2, j3;
ButtonListener bl1;
ButtonListener2 bl2;

public SinChecker ()
{ //Get the container
    Container c = getContentPane ();

    //Set absolute layout
    c.setLayout (null);

    //Set Background Color
    c.setBackground (Color.WHITE);


    //Creating label Guess my number text
    JLabel j = new JLabel ("Social Insurance Calculator");
    j.setForeground (Color.BLUE);
    j.setFont (new Font ("tunga", Font.BOLD, 24));
    j.setSize (270, 20);
    j.setLocation (30, 35);

    //Creating label Enter a number.....
    JLabel j1 = new JLabel ("Enter your S.I.N. below.");
    j1.setFont (new Font ("tunga", Font.PLAIN, 17));
    j1.setSize (270, 20);
    j1.setLocation (66, 60);

    //Creating a label Instuctions
    JLabel j2 = new JLabel ("Enter a 9-digit Social Insurance Number");
    j2.setFont (new Font ("tunga", Font.PLAIN, 17));
    j2.setSize (270, 20);
    j2.setLocation (10, 165);

    //Creating a label Instuctions
    JLabel j3 = new JLabel ("with no spaces between the digits please.");
    j3.setFont (new Font ("tunga", Font.PLAIN, 17));
    j3.setSize (270, 20);
    j3.setLocation (10, 180);



    //Creating TextField for x input guess
    t1 = new JTextField (10);
    t1.setSize (70, 30);
    t1.setLocation (100, 80);

    //creating 2 buttons
    JButton b1 = new JButton ("Proceed");
    b1.setSize (120, 30);
    b1.setLocation (70, 200);
    bl1 = new ButtonListener ();
    b1.addActionListener (bl1);


    JButton b2 = new JButton ("Re-enter");
    b2.setSize (120, 30);
    b2.setLocation (70, 250);
    bl2 = new ButtonListener2 ();
    b2.addActionListener (bl2);

    //Place the components in the pane
    c.add (j);
    c.add (j1);
    c.add (j2);
    c.add (j3);

    c.add (t1);
    c.add (b1);
    c.add (b2);

    //Set the title of the window
    setTitle ("Social Insurance Number Checker");

    //Set the size of the window and display it
    setSize (300, 350);
    setVisible (true);
    setDefaultCloseOperation (EXIT_ON_CLOSE);
}


//implement  first action listener
private class ButtonListener implements ActionListener
{

    public void actionPerformed (ActionEvent e)
    {
        int a = Integer.parseInt (t1.getText ());
        boolean evenDigit = false;   //alternates between true and false
        int sum = 0; //accumulates the sum of the digits (as modified)

        while (a > 0)
        {
            int nextDigit = a % 10; //grab the last digit
            a = a / 10; //discard that digit
            if (evenDigit)
            {
                //double it, then add the two digits of the result
                nextDigit = 2 * nextDigit;
                nextDigit = (nextDigit / 10) + (nextDigit % 10);
            } // if(evenDigit)
            sum = sum + nextDigit;
            evenDigit = !evenDigit; //toggle the flag each time
        } // end while

        if (0 == sum % 10)
        {
            j1.setText ("That is a valid S.I.N.");
        }
        else
        {
            j1.setText ("That is not a valid S.I.N.");

        }
        t1.requestFocus ();
        t1.selectAll ();
    }
}


private class ButtonListener2 implements ActionListener
{
    public void actionPerformed (ActionEvent e)
    {
        //reset the label messages
        t1.setText ("");


        t1.requestFocus ();
        t1.selectAll ();



    }
}


public static void main (String[] args)
{
    new SinChecker ();
}

}

Mike Smith
  • 19
  • 2
  • `static` and `null` layouts are not your friend – MadProgrammer Apr 17 '15 at 02:26
  • its **not** "spam out a ton of errors" , its **null pointer exception** @ j1.setText......... – Srinath Ganesh Apr 17 '15 at 02:31
  • 1
    See [Detection/fix for the hanging close bracket of a code block](http://meta.stackexchange.com/q/251795/155831) for a problem I could no longer be bothered fixing. – Andrew Thompson Apr 17 '15 at 02:34
  • 1) See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) 2) Always copy/paste error and exception output! – Andrew Thompson Apr 17 '15 at 02:38

2 Answers2

3

There are two problems...

  1. The user of static for what should be instance fields
  2. Variable shadowing

For example...

class SinChecker extends JFrame
{ //naming variables
    // This is a BAD idea
    static JLabel j, j1, j2, j3;

    public SinChecker ()
    { //Get the container
        //...
        // Now what does SinCheck.j1 actually equal?
        //Creating label Enter a number.....
        JLabel j1 = new JLabel ("Enter your S.I.N. below.");

j1 is re-declared as a local variable within the constructor of SinChecker, making SinCheker.j1 still null

Even if you fix this issue, you should then ask yourself the question, what happens if you create a second copy of SinCheck? Which label are you now referencing...

Start by removing the static reference and the declaration of your labels in the constructor

class SinChecker extends JFrame
{ //naming variables
    private JLabel j, j1, j2, j3;

    public SinChecker ()
    { //Get the container
        //...
        // Now what does SinCheck.j1 actually equal?
        //Creating label Enter a number.....
        j1 = new JLabel ("Enter your S.I.N. below.");

Avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify

Take a closer look at Laying Out Components Within a Container for more details

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
2

Your are re-declaring the JLabels in the method. You have already declared them as fields. And they become treated as local variables. Replace:

 JLabel  j1 = new JLabel ("Enter your S.I.N. below.");

with

  j1 = new JLabel ("Enter your S.I.N. below.");
Slow Trout
  • 492
  • 3
  • 13