0

我正在编写一个带有 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>
4

2 回答 2

0

MChart[] chart = new MChart[input.length+1];

这会分配一个nullMChart 对象数组。你立即参考chart[0].intialize("0", "ROOT S", "0,0", "-1", "Seed");

您首先要做的是chart[0] = new MChart()或类似的事情。显然,您需要对阵列中的每个插槽执行此操作。

于 2013-03-19T20:48:57.470 回答
0

只看java我想我可以看到问题

预留 100 个字符串的空间

private  String[] stateId = new String [100]; 

FIRST 并且只有第一个被初始化为 s 输入

stateId[0]  =   s;

0 以外的任何值都会给出空指针异常

public String getStateID(int i)
{
    return stateId[i];
}
于 2013-03-19T20:38:38.603 回答