程序中会出现
java.lang.NullPointerException的错误
我这里需要处理一个xls文件的内容,但是在运行的时候会出现java.lang.
NullPointerException的错误提示。我不知道哪里错了,请问有没有哪位大侠能帮我看一下。
package program;
import java.io.*;
import jxl.*;
import jxl.write.*;
public class tongyishibie {
	public static String fileToBeread = "F:/汉藏词典.xls";
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String[][] result = new String[100000][];
		int x=1;
		int y=0;
		try
		{
			Workbook book = Workbook.getWorkbook(new FileInputStream(fileToBeread));
			Sheet sheet = book.getSheet(0);
			int rownum = sheet.getRows();
			for(int i = 0;i<rownum;i++)
			{
				if(i>0)
				{
					Cell cell0 = sheet.getCell(1, i-1);
					String s0 = cell0.getContents();
					Cell cell1 = sheet.getCell(1, i);
					String s1 = cell1.getContents();
					if(s1==s0)
						continue;
					else
					{
						for(int j = i+1;j<rownum;j++)
						{
							Cell cell2 = sheet.getCell(1, j);
							String s2 = cell2.getContents();
							if(s1==s2)
							{								
							    Cell cell4 = sheet.getCell(2, j);
							    String s4 = cell4.getContents();
							    result[x][y++] = s4;
							}
							else
								continue;
						}
						Cell cell3 = sheet.getCell(2, i);
						String s3 = cell3.getContents();
					    result[x++][y] = s3;
					}
					y=0;
				}
				else
				{
					Cell cell1 = sheet.getCell(1, i);
					String s1 = cell1.getContents();
					for(int j = i+1;j<rownum;j++)
					{
						Cell cell2 = sheet.getCell(1, j);
						String s2 = cell2.getContents();
						if(s1==s2)
						{
						    Cell cell4 = sheet.getCell(2, j);
						    String s4 = cell4.getContents();
						    result[x-1][y++] = s4;
						}
						else
							continue;
						Cell cell3 = sheet.getCell(2, i);
						String s3 = cell3.getContents();
						result[x++][y++] = s3;
					}
				}				
			}
			for(int i = 0;i<=x;i++)
			{
				System.out.println("汉语词:"+result[i][result[i].length-1]);
				for(int j = 0;j<result[i].length-1;j++)
				{
					System.out.print("藏语词:"+result[i][j]);
				}
			}
			book.close();
		}catch(Exception e){System.out.println(e);}
		// TODO Auto-generated method stub
	}
}
------解决方案--------------------那句话报错总知道把,仔细看看错误信息,很有帮助的
------解决方案--------------------NullPointerException 的行号是啥?你没给出来。
请把:
 }catch(Exception e){System.out.println(e);}
修改为:
 }catch(Exception e){e.printStackTrace();}
然后观察里面提示中给出的行号,找到罪魁祸首的语句是哪句,然后增加判断来避免它。
------解决方案--------------------
2维数组要这样new的。
Java code
        String[][] result = new String[100000][];
        
        for (int i = 0; i < result.length; i++) {
            result[i] = new String[10];
        }
------解决方案--------------------
应该是有空指针没有初始化,仔细检查一下你的对象都初始化了没。