English 中文(简体)
为什么会把Null PointerException错误带给我的阵列?
原标题:Why do i get the NullPointerException error with my array?

我的方案运行。 在该方案的中期,它只是说:

Exception in thread "main" java.lang.NullPointerException
at Example.main(Example.java:33)

我的守则是:

String [] iArray ;
String i ;
int counter=1 ; counteragain=-1, q;

do {
System.out.print ("

Item Code : ") ;
i = s.next();

if (i.equals ("0") ){
  counter = 0 ;
} else {
  System.out.print ("
Quantity: ") ;
q = s.nextInt() ;
counteragain++ ;

i = iArray[counteragain];
}
} while (counter!=0) ;

The line where I m getting the error is:

 i = iArray[counteragain];

我为什么会发现这一错误? 我的同yn是否错了? 但是,为什么它管理我的方案?

任何帮助都会受到高度赞赏。

问题回答

You did not initialize this Array. Therefore it has a null value. You have to do something like this

String[] iArray = new String[numberOfElements];

You do not need to initialize the array in the declaration. So you can first recollect this numberOfElements and then initialize it.

iArray = new String[numberOfElements];

<条码> i Array ; 从未出现过。 因此,最初的<代码>iArray是无效的。

当你重新获得一个元素(i = iArray[counteragain];时,你再次试图进入一个未证实的阵列,因此投下了“NullPointerException。

我不知道贵方的阵容大小,但为了统一,你必须:

String[] iArray = new String[size];

现在,iArray将拥有一个长度size的持有人,但每个要素都将是null,因为其吨数被分配为价值。 i.e.,iArray[0]将无效,因为0没有价值。

you need to allocate the array. the line String [] iArray ; only declared a reference to an array of type string but did not allocate any room for objects in the array; you need to change that line to something like

String[] iArray = new String[size of needed array];

如果你能够预测所需规模,则最好使用阿雷拉迪这样的收集类型。

Your code is not complete, it does not show what s is (I guess it is an iterator) and where it came from. From the code you posted the Exception is the consequence of

String [] iArray;

你没有将伊阿雷拉开门,这样就没有了;你应当拿出一种工作方法/等级,以便获得正确的答案。

<代码>iArray = 新的Sting[n];,其中n中的要素数目。 Array。

如果帮助你删除<条码>NullPointerException。

www.un.org/Depts/DGACM/index_spanish.htm Array从没有开始。 因此,参考的<代码>iArray[counteragain]没有提及;因此,NullPointerException





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签