0

我正在制作一个简单的代码生成器(非随机),但遇到了问题。用户将数据输入到 7 个文本字段中;然后将其内容放入字符串类型的数组中。然后我的代码获取该数组,分析不同的元素并吐出一个可用的代码(由我)。

但是,在我尝试标准化字符串数组(也就是让每个元素都成为小写字符串)时,它会导致整个程序停止工作,并且我得到了一个空指针异常。

我的具体错误信息是这样的:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
 at inventorycodeconverter.codeDisplay.calculateData(codeDisplay.java:48)
    at inventorycodeconverter.InventoryCodeConverterView.button1ActionPerformed(InventoryCodeConverterView.java:315)
    at inventorycodeconverter.InventoryCodeConverterView.access$900(InventoryCodeConverterView.java:23)
    at inventorycodeconverter.InventoryCodeConverterView$5.actionPerformed(InventoryCodeConverterView.java:211)
    at java.awt.Button.processActionEvent(Button.java:392)
    at java.awt.Button.processEvent(Button.java:360)
    at java.awt.Component.dispatchEventImpl(Component.java:4630)
    at java.awt.Component.dispatchEvent(Component.java:4460)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

似乎给麻烦的代码部分是这样的:

public void calculateData(){

     int i;
     for (i = 0; i < calculationBuffer.length; i++){ // calculationBuffer is defined and initialized elsewhere.

         calculationBuffer[i] = calculationBuffer[i].toLowerCase();// this is line 48 where the first error appears to be.
     }


     if (calculationBuffer[0].equals("cap")){
           displayBuffer[0] = "CAP"; // displayBuffer is defined and initialized elsewhere.
     }
     else {
         displayBuffer[0] = "#Error#";
     }


     if (calculationBuffer[1].equals("20/400")) {
           theTA.append("B");
     }
     else if (calculationBuffer[1].equals("20/410")){
           displayBuffer[1] = "C";
     }
     else if (calculationBuffer[1].equals("24/410")){
         displayBuffer[1] = "F";
     }
     else if (calculationBuffer[1].equals("24/415")) {
         displayBuffer[1] = "Y";
     }
     else if (calculationBuffer[1].equals("28/400")){
         displayBuffer[1] = "H";
     }
     else if (calculationBuffer[1].equals("28/415")) {
         displayBuffer[1] = "X";
     }
     else if (calculationBuffer[1].equals("28/410")) {
         displayBuffer[1] = "I";
     }
     else if (calculationBuffer[1].equals("33/400")) {
         displayBuffer[1] = "J";
     }
     else if (calculationBuffer[1].equals("38/400")) {
         displayBuffer[1] = "K";
     }
     else if (calculationBuffer[1].equals("43/400")) {
         displayBuffer[1] = "M";
     }
     else if (calculationBuffer[1].equals("45/400")) {
         displayBuffer[1] = "N";
     }
     else if (calculationBuffer[1].equals("58/400")) {
         displayBuffer[1] = "Q";
     }
     else if (calculationBuffer[1].equals("70/400")) {
         displayBuffer[1] = "S";
     }
     else if (calculationBuffer[1].equals("89/400")) {
         displayBuffer[1] = "T";
     }

 }


 public void displayData() {
      String displayString;
      displayString = Arrays.toString(displayBuffer);
      StringTokenizer st = new StringTokenizer(displayString, ",");

      while(st.hasMoreTokens()){
          String finalText = st.nextToken();

          theTA.append(finalText);
      }
 }
4

3 回答 3

4

在某处您已将数组声明如下(可能):

String[] calculationBuffer = new String[size];

它将用nulls 初始化所有索引。因此你得到了 NPE。

在使用数组之前,您应该如下填充循环中的所有索引

for(int i=0; i<size; i++)
{
    calculationBuffer[i] = "A String";
}
于 2013-01-24T06:37:44.743 回答
2

如果你像这样声明你的字符串数组String[] calculationBuffer = new String[size];

然后所有字符串元素都由 Null 初始化,所以如果你没有在其他地方的这个字符串数组中添加值,那么你可以像这样更新你的代码

for (i = 0; i < calculationBuffer.length; i++){ 
      **calculationBuffer[i]="";**
     calculationBuffer[i] = calculationBuffer[i].toLowerCase();// this is line 48 where the first error appears to be.
 }

谢谢

于 2013-01-24T06:48:02.700 回答
0

你在哪里填写你的计算缓冲区?

您得到的错误表明calculationBuffer[i] 为空,这意味着您没有正确填充初始数组。

于 2013-01-24T06:33:05.477 回答