• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

string array in while loop not wirking

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok i got it the error is i did not initialized the string array properly.

Thank's
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
manohar gunturu
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 254
1
MySQL Database Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 20
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic