我正在编写一个带有java类的JSP程序,并试图构建一个对象数组,但是得到了NullPointerException。请理清我的问题。
我的java类MChart.java
如下:
package myPackages;
public class MChart
{
private String[] stateId = new String [100];
private String[] rule = new String[100];
private String[] dotPosition = new String[100];
private String[] backPointer = new String [100];
private String[] operation = new String[100];
public void intialize (String s, String r, String dp, String bp, String o)
{
stateId[0] = s;
rule[0] = r;
dotPosition[0] = dp;
backPointer[0] = bp;
operation[0] = o;
}
public String getStateID(int i)
{
return stateId[i];
}
public String getRule(int i)
{
return rule[i];
}
public String getDotPosition(int i)
{
return dotPosition[i];
}
public String getBackPointer(int i)
{
return backPointer[i];
}
public String getOperation(int i)
{
return operation[i];
}
}
现在我的jsp文件如下:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@ page import="myPackages.MChart"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" >
<title>QASKU URDU PARSER</title>
<!-- scripting code is deleted -->
</head>
<body>
<form name="myform" method="get" >
<div align="center" onKeyPress="return checkSubmit(event)">
<h4>QASKU URDU PARSER</h4><br>
<h5>Type sentence using Urdu/Arabic script only and then press the 'Parse' button below</h5><br>
<textarea cols="100" rows="5" style="text-align: right" name="mytextarea" id="message" >Type here</textarea>
<script type="text/javascript"> blurfocus("message"); </script>
<br><br>
<input type="submit" value="Parse" >
</div>
</form>
<%
try
{
if ( request.getParameter("mytextarea") == null)
{
out.println("<p align='center'>No Any Input Yet</p>");
}
else
{
<!-- database entries are deleted -->
// inputting sentence from user and splitting into an array of strings
String sent = request.getParameter("mytextarea");
String[] input = sent.split(" ");
//initialize chart and root rule
MChart[] chart = new MChart[input.length+1];
// only beginning instance is initiated and the rest will be initiated on
runtime // This also the NULL pointer creating statement
chart[0].intialize("0", "ROOT S", "0,0", "-1", "Seed");
out.println("<p><table align=center border='1' bgcolor='green' width='75%'><col width='25'><col width='25'><col width='25'><col width='25'><col width='25'>");
out.println("<tr><th align=left>STATEID</th><th align=left>RULE</th><th align=left>DOT-POSITON</th><th align=left>BACK-POINTER</th><th align=left>OPERATION</th></tr>");
out.println("<tr><td align=left>"+chart[i].getStateID(i)+"</td>");
out.println("<td align=left>"+chart[i].getRule(i)+"</td>");
out.println("<td align=left>"+chart[i].getDotPosition(i)+"</td>");
out.println("<td align=left>"+chart[i].getBackPointer(i)+"</td>");
out.println("<td align=left>"+chart[i].getOperation(i)+"</td></tr>");
out.println("</table></p>");
}
catch (Throwable any)
{
out.println("<p align=center>Java ERROR: "+any+"</p>");
any.printStackTrace();
}
%>
</body>
</html>
答案 0 :(得分:0)
只看java我认为我可以看到问题
保留100个字符串的空间
private String[] stateId = new String [100];
FIRST,只有第一个被初始化为s输入
stateId[0] = s;
除0以外的任何值都将给出空指针异常
public String getStateID(int i)
{
return stateId[i];
}
答案 1 :(得分:0)
MChart[] chart = new MChart[input.length+1];
这将分配null
个MChart对象的数组。您可以立即参考chart[0].intialize("0", "ROOT S", "0,0", "-1", "Seed");
您首先要做的是chart[0] = new MChart()
或其他类似的事情。显然你需要为数组中的每个插槽执行此操作。