-2

我对Java(和这个网站)相当陌生,所以我不确定如何处理这个“问题”......我不确定是否将我的整个代码放入这个......所以我'将只是提出我认为导致问题的原因。该程序编译完美,除非我尝试运行它。那是我在第 96 行收到空指针异常的时候。我不知道为什么会这样。

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

public class LicensePlateGeneratorTemplate extends JFrame implements ActionListener{

  private JTextArea txtOutput;
  private JTextField txtNumPlates;
  private JButton btnDisplay;
  private JLabel lblTitle, lblInstruction;
  private PrintWriter output;
  private String letters, numbers, fileName;
  private int randomLetter, randomNumber, numPlates;
  private String [] a, b ;
  private String [] lttrArray, nmbrArray;

  public static void main(String[] args) throws IOException{

new LicensePlateGeneratorTemplate();

}    
  public LicensePlateGeneratorTemplate() throws IOException{
    //label thast contains number of licence plates generated
    txtNumPlates = new JTextField();
    txtNumPlates.setBounds(320, 60, 28, 28);
    lblInstruction = new JLabel("How many license plates would you like to generate?");
    lblInstruction.setBounds(15, 60, 300, 28);

//A random number generator in the action performed method should allow you to select a random letter from the letters String
//then a random number from the numbers String

String letters = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Z";
String numbers = "1,2,3,4,5,6,7,8,9,0";

lttrArray = letters.split (",");
nmbrArray = numbers.split (",");

System.out.println(lttrArray[20] + lttrArray [10]);
System.out.println(nmbrArray[3]+ nmbrArray [5]);

fileName = "licensePlates.txt";
output = new PrintWriter(new FileWriter(fileName));

lblTitle = new JLabel();
lblTitle.setFont(new Font("Britannic Bold", Font.BOLD, 28));
lblTitle.setText("License Plate Generator");
lblTitle.setBounds(30, 10, 400, 50);

JPanel panel = new JPanel();
panel.setLayout(null);

//JTextArea
txtOutput = new JTextArea();
txtOutput.setBounds(60, 150, 275, 196);
txtOutput.setEditable(true);

//generate button properties
btnDisplay = new JButton();
btnDisplay.setText("Generate");
btnDisplay.setBounds(150, 100, 100, 40);
btnDisplay.setFocusable(true);
btnDisplay.addActionListener(this);

//add components to the panel
panel.add(btnDisplay);
panel.add(txtOutput);
panel.add(lblTitle);
panel.add(lblInstruction);
panel.add(txtNumPlates);

//frame properties
setContentPane(panel);
setSize(400, 400);
setTitle("License Plate Generator");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
  }

  public void actionPerformed (ActionEvent e) 
  {
    if (e.getSource() == btnDisplay)
    {
     //change number user inputs into a int value
      int userNum = Integer.parseInt(txtNumPlates.getText ());

 for (int k = 0; k <= userNum; k ++)
  {//number of licence places that are being made

    for (int i = 0; i < 4; i ++)//loop for letters produced
    {
      int randLtr = 0 + (int)(Math.random() * ((24 - 0) + 1));
      a [i]  = lttrArray [randLtr];
      output.println (a[i]);
      txtOutput.insert(a[i] + "\n", 0);
    }

    for (int j = 0; j < 3; j++)//loop for number produced
    {
      int randNum = 0 + (int)(Math.random() * ((9 - 0) + 1));  
      b [j] = nmbrArray [randNum];
      output.println (b[j]);
      txtOutput.insert(b[j] + "\n", 0);
    }
    output.close();//closes fileWriter
  }//end of userNum loop
}//end of e.getsource
  }  
}

编辑:添加了完整的代码。

4

1 回答 1

0

由于lttrArray显然是非空的(除非您将其分配为lttrArray = null剪切代码中的某个位置),我猜它a是空的。

于 2013-05-29T19:57:23.290 回答