Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I get array index out of bounds exception when trying to read data from a file into an array. The file has 700 lines data similar to:

"Vasculitis_PlasmaExchange", "#FCE883", "http://ncbi.nlm.nih.gov/pubmed/18646089", "(252, 232, 131)"

"Vasculitis_Prednisone", "#C5E384", "http://ncbi.nlm.nih.gov/pubmed/19588365", "(197, 227, 132)"

my code is:

static{  
    COLOR_CODES = new ArrayList<String[]>();

    try{ 
        FileReader fr = new FileReader("Crayon.properties");
        BufferedReader br = new BufferedReader(fr);
        String line;

        while ( (line = br.readLine()) != null) {       

            COLOR_CODES.add(new String[]{line});               
        } 
        br.close();
        fr.close();

    }catch (Exception e){
        throw new IllegalStateException("Couldn't load array file");
    }       
}

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException,
        IOException
{
    JSONArray fullColorArray;
    String query = request.getParameter("q");

    try {
        int count = 0;
        if (query.equals(m_lastQuery)) {
            fullColorArray = m_lastResults;
            count = m_lastResults.length();
        } else {
            m_lastQuery = query;

            fullColorArray = new JSONArray();
            for (String[] colorCode : COLOR_CODES) {
                String colorName = colorCode[0];
                String lowerColor = colorName.toLowerCase();
                int has = lowerColor.indexOf(query.toLowerCase());

                if (!query.isEmpty() && (query.equals("*") || has >= 0)) {
                    JSONObject color = new JSONObject();
                    color.put("DisplayName", colorName);
                    color.put("Value", colorCode[1]); // <-------- ArrayIndexOutOfBoundsException
                    color.put("Description", colorCode[2]);
                    color.put("RGB", colorCode[3]);
                    fullColorArray.put(color);
                    count++;
                }
            }
            m_lastResults = fullColorArray;
        } 

NullPointerException:

HTTP ERROR: 500

INTERNAL_SERVER_ERROR

RequestURI=/multivaluesuggestboxexample/colors

Caused by:

java.lang.NullPointerException at org.spiffyui.spiffynavigation.server.CrayonColorsServlet.doGet(CrayonColorsServlet.java:95) at javax.servlet.http.HttpServlet.service(HttpServlet.java:707) at javax.servlet.http.HttpServlet.service(HttpServlet.java:820) at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487) at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:362) at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216) at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181) at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:726) at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405) at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:206) at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114) at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152) at org.mortbay.jetty.Server.handle(Server.java:324) at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505) at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:829) at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:514) at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:211) at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380) at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:395) at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:488)

(Based on CrayonColorsServlet.java)

Please help.

AM Mohan Rao

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
716 views
Welcome To Ask or Share your Answers For Others

1 Answer

I think you want something like :

 while ( (line = br.readLine()) != null)        
      COLOR_CODES.add(line.split(","));               

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...