Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2006

Tip: Looking for answers? Try searching our database.

String array not recognized

Thread view: 
mcraven.2@wright.edu - 12 Jul 2006 21:57 GMT
I'm having trouble debugging some code that I created in JSP.  Advice
on the problem itself or how I can go about debugging it would be
appreciated.  The way that I debug my code now is I change the JSP
webpage and then look at it through a browser to see if there is an
error in the code.

The error that I'm getting is I'm trying to create an SQL statement
with the multiple table columns and values in the statement.  So a
pseudo statement would be INSERT INTO table (col1,col2,col3) VALUES
(val1,val2,val3).  The names of the columns (col1,col2...) come from a
tokenized string which I tokenize into an array of strings.  So I have
a statment like this:

 tokens = new java.util.StringTokenizer(MM_columnsStr,"|");
 MM_columns = new String[tokens.countTokens()];
 for (int i=0; tokens.hasMoreTokens(); i++) MM_columns[i] =
tokens.nextToken();

Where I run into an error is that I try to create a columns_string that
in the end will have the value of "col1,col2,col3".  To create this
string I first need to assign "col1" to columns_string.  I've tried
statements like

  columns_string = MM_columns[0];
  columns_string.append(MM_columns[0]);
  columns_string.append(MM_columns);
  columns_string = MM_columns[1];

but I keep on getting a null pointer exception.  I have declared
columns_string correctly because I can put in a simple dummy string and
the code works.  The MM_columns[0] work on another set of my forms it
is getting initialized correctly at least in my other forms.

Does anyone have any ideas on how I can trouble shoot this?  Anything
stick out as missing in my code?

Brian
Oliver Wong - 12 Jul 2006 22:06 GMT
> I'm having trouble debugging some code that I created in JSP.  Advice
> on the problem itself or how I can go about debugging it would be
> appreciated.  The way that I debug my code now is I change the JSP
> webpage and then look at it through a browser to see if there is an
> error in the code.

   Here's some general tips:
http://riters.com/JINX/index.cgi/Suggestions_20for_20Asking_20Questions_20on_20N
ewsgroups#RepeatErrorsExactly


> The error that I'm getting is I'm trying to create an SQL statement
> with the multiple table columns and values in the statement.  So a
[quoted text clipped - 25 lines]
> Does anyone have any ideas on how I can trouble shoot this?  Anything
> stick out as missing in my code?

   NullPointerException (NPE) means something is null when it shouldn't be.
It's important that you find out exactly what line this NPE is occuring on.
See the link I mentioned above for how to diagnose the stack trace to
determine which line the problem lies on.

   If you're still stuck, post that line and some surrounding code for us
to take a look at.

   - Oliver
mcraven.2@wright.edu - 13 Jul 2006 13:36 GMT
I've looked at the error message.  The line it appears to point at is
down a ways from where I'm finding the bug.  I know the bug is where I
think it is by commenting out the specific line and getting no error.
What I have is a web page with a form on it.  When a submit button is
hit the same web page loads but this time with a hidden value set.
When this hidden value is set the following simplified version of my
code is run:

<%
if (request.getParameter("MM_update") != null ) {

 MM_editQuery = new StringBuffer("INSERT INTO
").append(MM_editTable).append(" (");
 String columns = new String();
 columns = MM_columns[1];  // this is where the error is at
 // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

 // more lines of code
}
%>
<%
// create one result set (no conditions for this running)
%>
<% // create the next result set (no conditions)
// this is where the trace points a null pointer exception
Driver DriverrsDataSubj =
(Driver)Class.forName(MM_EDMREPO_DRIVER).newInstance();
// more lines of code
%>

It would seem to be a simple problem of assigning a string array
element to a string in Java.

I almost forgot to put in the error message.  This is the first block:

org.apache.jasper.JasperException
    at
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:248)
    at
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)

And heres the trace:

java.lang.NullPointerException
    at
org.apache.jsp.businessentity_0002dadd_jsp._jspService(businessentity_0002dadd_jsp.java:146)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:137)

Brian

> > I'm having trouble debugging some code that I created in JSP.  Advice
> > on the problem itself or how I can go about debugging it would be
[quoted text clipped - 44 lines]
>
>     - Oliver
mcraven.2@wright.edu - 13 Jul 2006 14:05 GMT
I think what I'm finding is that this value is null:  MM_columns[1];

So I'll show again where this array gets initialized in this segment of
code further up in the code:

 String MM_columnsStr =
"business_entity_id|none,none,NULL|business_entity_type_id|..."

 tokens = new java.util.StringTokenizer(MM_columnsStr,"|");
 MM_columns = new String[tokens.countTokens()];
 for (int i=0; tokens.hasMoreTokens(); i++) MM_columns[i] =
tokens.nextToken();

I added the java.util.* class to my JSP at the top in hopes that the
problem was that the tokenizer wasn't working but I get the same error.
So the top line looks like this:

<%@ page contentType="text/html; charset=windows-1252" language="java"
import="java.sql.*,java.util.*" %>

I know I'm throwing all kinds of lines of code around but I think I'm
on the right track.

Brian

> I've looked at the error message.  The line it appears to point at is
> down a ways from where I'm finding the bug.  I know the bug is where I
[quoted text clipped - 96 lines]
> >
> >     - Oliver
mcraven.2@wright.edu - 13 Jul 2006 14:59 GMT
I found the problem.  The tokenizing and intializing of the
MM_columns[] array had a condition on it that I needed to modify.  I'm
working with older code and I didn't think to look this over.  The
tokenizing etc now has the same condition placed on it as is placed on
the segment that had the null pointer exception.  Namely the JSP page
is only looking to see if the form has been up before.  I may need to
improve but that is coding I can do.

Brian

> I think what I'm finding is that this value is null:  MM_columns[1];
>
[quoted text clipped - 121 lines]
> > >
> > >     - Oliver
Andrew T. - 13 Jul 2006 15:00 GMT
...
> I added the java.util.* class to my JSP at the top in hopes that the
> problem was that the tokenizer wasn't working but I get the same error.
..
> I know I'm throwing all kinds of lines of code around but I think I'm
> on the right track.

..think again.  I suggest you put aside the JSP's for the moment
and develop some Java programs meant to be run from the command
line, until you get the hang of the language.  Command line programs
are much easier to debug than anything coming from a server.

Andrew T.
Paul Davis - 13 Jul 2006 17:00 GMT
Also, after doing as Andrew suggested.
Move the code into a servlet, then store the output data in the request
object and dispatch to the jsp (to display).
You really want to avoid putting a lot of  (any) java in your JSP
because, its hard to debug, maintain, and re-use.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.