我的程序中是否为空指针异常?

时间:2016-03-20 01:47:58

标签: java nullpointerexception

尝试运行我的程序时,NPE出现了。当我尝试在标签上设置文本时,我不确定为什么会出现空指针异常?有人可以向我解释为什么会这样吗?由于我对编程有点新,我并不完全熟悉这些例外。谢谢!

编辑:向前走,将**线__ **放在发生错误的地方

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);
  }
 }

0 个答案:

没有答案