0

While attempting to run my program an NPE appeared. I'm not sure why I am getting a null pointer exception when I try to set text on a label? Can someone explain to me why this is happening? As I'm somewhat new to programming, I'm not entirely familiar with these exceptions. Thanks!

EDIT: Went ahead and put **Line __ ** where the errors occurred

Exception in thread "main" java.lang.NullPointerException
at Display10.convert(Display10.java:40)
at Display10.randomize(Display10.java:49)
at Display10.<init>(Display10.java:18)
at Panel10.<init>(Panel10.java:14)
at Driver10.main(Driver10.java:13)

Display10

import javax.swing.*;
import java.awt.*;
public class Display10 extends JPanel
{
  private JLabel[] bits;
  private JLabel label; 
  public Display10()
  {
     setLayout(new GridLayout(1, 8));
     bits = new JLabel[8];
     for(int x = 0; x < bits.length; x++)
     {
        bits[x] = new JLabel("", SwingConstants.CENTER);
        bits[x].setFont(new Font("Serif", Font.BOLD, 50));
        add(bits[x]);
     }
     randomize(); **Line 18**
     label = new JLabel("", SwingConstants.RIGHT);
     label.setFont(new Font("Serif", Font.BOLD, 20));
     add(label); 
     label.setText("placeholder"); 
     convert();  

  }

  private void convert()
   {
    double[] digit = new double[8];
    double count = 0;
    int power = 0; 
    for(int x = 0; x < digit.length; x++)
     {
      digit[x] = 0.0; 
      digit[x] = Double.parseDouble(bits[x].getText()); 
      digit[x] = digit[digit.length - 1] * Math.pow(2, power); 
      power++; 
      count = count + digit[x]; 
     }
     label.setText("" + count); **Line 40**
   }

  public void randomize()
  {
    for(int x = 0; x < bits.length; x++)
     {
        bits[x].setText("" + (int)(Math.random() * 1 + 0.5));  
     } 
     convert(); **Line 49**
  }
  public void reverse()
  {
       JLabel[] temp = new JLabel[8]; 
       for(int x = 0; x < bits.length; x++)
        {
         temp[x] = new JLabel(); //fills array with JLabels, prevents null pointer exception
         temp[x].setText("" + bits[x].getText()); //temporary storage array
        }

       for(int x = 0; x < temp.length / 2; x++) //reverse the array for length/2 to prevent reswapping
        {
          bits[x].setText("" + temp[temp.length - 1 - x].getText());
          bits[bits.length - 1 - x].setText("" + temp[x].getText()); //sets the other place to the first text
         }
        convert(); 

  }
  public void shift()
  {
        for(int x = 0; x < bits.length - 1; x++)
         {
         bits[x].setText("" + bits[x+1].getText());
         bits[bits.length-1].setText("" + 0); 
         }
       convert(); 

  }
  public void rotate()
  {
    int place = Integer.parseInt(bits[0].getText());
    for(int x = 0; x < bits.length - 1; x++)
     {
     bits[x].setText("" + bits[x+1].getText());
     }
    for(int x = 0; x < bits.length; x++)
     {
     bits[bits.length - 1].setText("" + place); 
      } 
    convert(); 
   }    

}

Panel10

  import javax.swing.*;
  import java.awt.*;
  import java.awt.event.*;
  public class Panel10 extends JPanel
  {
   Display10 display;
   public Panel10()
  {
     setLayout(new BorderLayout());

     display = new Display10(); **Line 14**
     add(display, BorderLayout.CENTER);

     JPanel panel = new JPanel();
     panel.setLayout(new FlowLayout());
     add(panel, BorderLayout.SOUTH);

     JButton button1 = new JButton("Randomize");
     button1.addActionListener(new Listener1());
     panel.add(button1);

     JButton button2 = new JButton("Reverse");
     button2.addActionListener(new Listener2());
     panel.add(button2);

     JButton button3 = new JButton("Shift");
     button3.addActionListener(new Listener3());
     panel.add(button3);

     JButton button4 = new JButton("Rotate");
     button4.addActionListener(new Listener4());
     panel.add(button4);
  }
   private class Listener1 implements ActionListener
  {
      public void actionPerformed(ActionEvent e)
     {
        display.randomize();  
     }
  }
   private class Listener2 implements ActionListener
  {
      public void actionPerformed(ActionEvent e)
     {
         display.reverse(); 
     }
  }
   private class Listener3 implements ActionListener
  {
      public void actionPerformed(ActionEvent e)
     {
        display.shift(); 
     }
  }
   private class Listener4 implements ActionListener
  {
      public void actionPerformed(ActionEvent e)
     {
        display.rotate(); 
     }
  }

}

Driver 10

   import javax.swing.JFrame;
   public class Driver10
   {
    public static void main(String[] args)
    {
     JFrame frame = new JFrame("Unit4, Lab10: Binary Reversal");
     frame.setSize(400, 180);
     frame.setLocation(200, 100);
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setContentPane(new Panel10()); **Line 13** 
     frame.setVisible(true);
  }
 }
ak_27
  • 27
  • 8
  • Why dont you tell us what line in the code is causing the error. – pczeus Mar 20 '16 at 01:49
  • The heuristic for NullPointerExceptions is almost always the same: You should critically read your exception's stacktrace to find the line of code at fault, the line that throws the exception, and then inspect that line carefully, find out which variable is null, and then trace back into your code to see why. You will run into these again and again, trust me. – Hovercraft Full Of Eels Mar 20 '16 at 01:53
  • 2
    `randomize` is called before the constructor initializes the label, but the label is used inside the `convert` method which is called from the `randomize` method. – fabian Mar 20 '16 at 01:56
  • Exactly what @fabian says is right. Create your JLabel object right away, certainly before calling `randomize()`. – Hovercraft Full Of Eels Mar 20 '16 at 02:02

0 Answers0