[英]Passing string array as arguements is not giving expected result
I m working on a program, where I retrieve string from db and store into a String array. 我正在开发一个程序,在该程序中,我从db中检索字符串并将其存储到String数组中。 Now I pass this array to method in another class.
现在,我将此数组传递给另一个类中的方法。 Say The string array length is 10. Now I will check for particular occurence of a char and I will do some work.
假设字符串数组的长度为10。现在,我将检查char的特殊出现,并做一些工作。 Now my problem is I am able to store the value from db into string array.
现在我的问题是我能够将db中的值存储到字符串数组中。 But what problem I am getting is giving null pointer exception.
但是我遇到的问题是给空指针异常。 As the loop which reads the string array in another method reads the last index array.
作为在另一个方法中读取字符串数组的循环,将读取最后一个索引数组。
The program: 该程序:
This is where I store the Db values into array. 这是将Db值存储到数组中的位置。 These arrays are global declarations.
这些数组是全局声明。
while(rst.next())
{
allitems[i]=rst.getString("transitems");
alltuv[i]=rst.getString("tuv");
}
Now pass this array into a method of same class. 现在将此数组传递给相同类的方法。
Window2 n=new Window2();
n.calculateTWU(allitems,alltuv);
Now I pass this array into another method of different class. 现在,我将此数组传递给另一个不同类的方法。
CalculateTWU ctw=new CalculateTWU();
String avalue=ctw.calctA(s1, s2,"A"); //s1 and s2 are the string arrays s1=allitems and s2=alltuv.
The method in another class say Class B 另一类中的方法称为B类
public String calctA(String[] a1,String[] a2,String indexstr)
{
for(int i=0;i<10;i++)
{
int is= a1[i].indexOf(indexstr);
if(is==-1)
{
}
else
{
valadd=Integer.parseInt(a2[i])+valadd;
}
}
return String.valueOf(valadd);
} }
Stack Trace : 堆栈跟踪 :
MNOETTAOHX // these are strings
BOCOPVBLEV
MIAISEUECV
MGUXPXPEDX
EQMWLRVKZG
MNFBFAXQNT
YZKDHMVSSA
java.lang.NullPointerException
at datasets.CalculateTWU.calctA(CalculateTWU.java:27)
at datasets.Window2.calculateTWU(Window2.java:205)
at datasets.Window2.jButton1ActionPerformed(Window2.java:146)
at datasets.Window2.access$000(Window2.java:20)
at datasets.Window2$1.actionPerformed(Window2.java:68)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
When you create an array, by default all it's items are null
until you populate them. 创建数组时,默认情况下,在填充它们之前,所有项目均为
null
。 In your example, you have 9 strings, but are looping on 10 items in the array. 在您的示例中,您有9个字符串,但是在数组中的10个项目上循环。 The last one is
null
, and hence your NullPointerException
. 最后一个为
null
,因此为NullPointerException
。 You can avoid this by explicitly checking: 您可以通过显式检查来避免这种情况:
public String calctA(String[] a1, String[] a2, String indexstr) {
for(int i=0; i < a1.length; ++i) {
if (a1[i] != null) {
int is= a1[i].indexOf(indexstr);
// rest of your code...
I think the problem lies here: 我认为问题出在这里:
for(int i=0;i<10;i++) \\ What line is this in the class?
change to 改成
for(int i =0; i < a1.length;i++)
The problem with your stack trace is since the whole class isn't present it's hard to determine where exactly the NullPointerException is occurring. 您的堆栈跟踪的问题是,由于不存在整个类,因此很难确定NullPointerException发生的确切位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.