File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes string array in while loop not wirking Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "string array in while loop not wirking" Watch "string array in while loop not wirking" New topic
Author

string array in while loop not wirking

manohar gunturu
Greenhorn

Joined: Jun 04, 2013
Posts: 26
my code as given below

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.*"%>
<%@page import="java.util.*"%>
<%@page import="java.io.*"%>

<%!
Connection conn=null;
ResultSet rs=null;
Statement stmt=null;
%>

<%
String email="mounika@yahoo.com";
String gunt="SELECT * from sony.frndreq where to='"+email+"' or from='"+email+"'";
String[] anArray=null;
int f=1;

try{
Class.forName("com.ibm.db2.jcc.DB2Driver");
conn=DriverManager.getConnection("jdbc:db2://localhost:50000/mail","db2admin","db2admin");

stmt=conn.createStatement();
rs=stmt.executeQuery(gunt);
while(rs.next()){
anArray[f]=rs.getString("from");
out.println(anArray[f]);

f++;
}
}
catch(SQLException e) {
out.println(e); }


%>


and error is as fallowing
org.apache.jasper.JasperException: An exception occurred processing JSP page /testing.jsp at line 25

22: stmt=conn.createStatement();
23: rs=stmt.executeQuery(gunt);
24: while(rs.next()){
25: anArray[f]=rs.getString("from");
26: out.println(anArray[f]);
27:
28: f++;


Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:521)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:430)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
root cause

java.lang.NullPointerException
org.apache.jsp.testing_jsp._jspService(testing_jsp.java:82)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:723)

please help of how to overcome this
manohar gunturu
Greenhorn

Joined: Jun 04, 2013
Posts: 26
ok i got it the error is i did not initialized the string array properly.

Thank's
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61414
    
  67

Do you realize that by using scriptlet declarations that only one person can use your site at a time without conflict?

Why are you putting Java code into a JSP page in 2013 when doing so has been obsolete since 2002?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
manohar gunturu
Greenhorn

Joined: Jun 04, 2013
Posts: 26
Bear Bibeault wrote:Do you realize that by using scriptlet declarations that only one person can use your site at a time without conflict?

Why are you putting Java code into a JSP page in 2013 when doing so has been obsolete since 2002?


Thank you .
Can you please tell to me how to write this code in a correct manner or how to solve the problem of only one person can use the site at a time with conflict.

Thanks for any help.
Ahsan Bagwan
Ranch Hand

Joined: Oct 05, 2010
Posts: 161
Use of scriptlets here is frowned upon. The friendly folks here have compiled an article describing all the bad things that can happen when you use Scriptlets ( <- link )
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61414
    
  67

You should be doing database access in a Java class, not a JSP. In a JSP, you should only be using the JSTL and EL; not Java scriptlets.

Please read this article for information on properly structuring web apps.
Ravi Khadgi
Greenhorn

Joined: Aug 06, 2010
Posts: 20

As everyone suggested, you should not be writing any java code inside a JSP. The problem with your posted code is that you have not instantiated the String array variable "anArray".

String[] anArray=null;
....
....
anArray[f]=rs.getString("from"); (NullPointerException will be thrown here as anArray is null)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: string array in while loop not wirking