简体   繁体   English

我可能有一个空指针异常,我只是不知道在哪里

[英]I possibly have a Null Pointer Exception, I just don't know where

I am making a simple- code generator (non-random) and I've run into a problem. 我正在制作一个简单代码生成器(非随机),但遇到了问题。 The user inputs data into 7 textfields; 用户将数据输入到7个文本字段中。 then contents of which are then placed into an array of type String. 然后将其内容放入String类型的数组中。 My code then takes that array, analyses the different elements and spits out a code that is usable (by me). 然后,我的代码获取该数组,分析不同的元素,并吐出一个可用的代码(对我来说)。

However, in my attempt to standardize the String array (aka make every element a lowercase String), it causes the entire program to stop working and I get what appears to be a null pointer exception. 但是,在尝试标准化String数组(也就是将每个元素都设置为小写String)时,它导致整个程序停止工作,并且出现了空指针异常。

My specific error message is as such: 我的特定错误消息如下:

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)

The section of code that appears to be giving the trouble is goes as such: 似乎造成麻烦的代码部分是这样的:

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

Somewhere you have declared you array as following (probably): 在某处您已声明数组如下(可能):

String[] calculationBuffer = new String[size];

It'll intialize all the indexes with null s. 它将使用null初始化所有索引。 and hence you are getting NPE. 因此,您将获得NPE。

You should fill all the indexes in a loop as following, before using the array 在使用数组之前,应按如下所示将所有索引填充到循环中

for(int i=0; i<size; i++)
{
    calculationBuffer[i] = "A String";
}

If you are declaring your string array like this String[] calculationBuffer = new String[size]; 如果要像这样声明字符串数组,则String[] calculationBuffer = new String[size];

then all string element are initialized by Null so if you are not adding value in this string array at somewhere else then you can update your code like this 那么所有的字符串元素都将由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.
 }

Thanks 谢谢

where do you fill your calculationBuffer ? 您在哪里填充您的CalculationBuffer?

the error you get indicated that calculationBuffer[i] is null meaning you didnt correctly fill your initial array. 您得到的错误指示CalculationBuffer [i]为null,表示您没有正确填充初始数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM