Wednesday, May 23, 2012

Re: Null pointer

To add to what Thad said, your map should look like this "Map<String, String> formData" since the key of the map looks like a String, and your just doing a toString on the value. So when you create your map it should probably look something like this.

Map<String, String> formData = new HashMap<String, String>();

It's really important that you type any collection object (Map, Set, List, ...) since your compiled code will be huge, and it save a lot of casting of objects. Not to mention being able to easily loop through collections using the a for-each loop like so.

List<String> items = new ArrayList<String>();
//add items to the list.

for(String item : items)
     print(item);

Lastly using the debugger is your friend. The possible items that could be null are formData, formData.get("username"), and stmt. My guess is that either your map is null, or your map doesn't contain "username".

Trevor

On Wednesday, May 23, 2012 2:12:22 AM UTC-5, SCK wrote:
Hi

 I try to connect to my database with GWT RPC. But i get a error.

Help please

Thks



java.lang.NullPointerException
        at
org.etude.gwt.server.DatabaseServiceImpl.saveData(DatabaseServiceImpl.java:
37)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at
com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:
569)
        at
com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:
208)
        at
com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:
248)
        at
com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:
62)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
        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:
729)
        at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:
405)
        at
org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:
152)
        at
org.mortbay.jetty.handler.RequestLogHandler.handle(RequestLogHandler.java:
49)
        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.content(HttpConnection.java:843)
        at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:647)
        at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:205)
        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)





package org.etude.gwt.server;

import java.net.URLDecoder;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.Map;

import org.etude.gwt.client.DatabaseService;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class DatabaseServiceImpl extends RemoteServiceServlet
implements
                DatabaseService {

        /**
         *
         */
        private static final long serialVersionUID = 1L;

        @SuppressWarnings("rawtypes")
        @Override
        public void saveData(Map formData) throws IllegalArgumentException {

                try{
                        Class.forName("com.mysql.jdbc.Driver");
                        Connection conn = DriverManager.getConnection("jdbc:mysql://
localhost:3306/oracle", "root", "");

                        Statement st = conn.createStatement();

                        //avec décodage
                        StringBuffer sqlQuery = new StringBuffer("insert into
gwtusers(username,password,email,phone)");
                        sqlQuery.append("values(?,?,?,?)");

                        PreparedStatement stmt =
conn.prepareStatement(sqlQuery.toString());
        LINE 37
stmt.setString(1,URLDecoder.decode(formData.get("username").toString(),"UTF-8"));
        
stmt.setString(2,URLDecoder.decode(formData.get("password").toString(),"UTF-8"));
        
stmt.setString(3,URLDecoder.decode(formData.get("email").toString(),"UTF-8"));
        
stmt.setString(4,URLDecoder.decode(formData.get("phone").toString(),"UTF-8"));
                        stmt.execute();

                        //sans décodage
                        st.executeQuery("insert into gwtusers values
('"+formData.get("username").toString()+
                                        "','"+formData.get("password").toString()+
                                        "','"+formData.get("email").toString()+
                                        "','"+formData.get("phone").toString()+"')");

                        conn.close();
                        //stmt.close();
                        //st.close();


                }catch(Exception e){
                        e.printStackTrace();
                }

        }

}

On Wednesday, May 23, 2012 2:12:22 AM UTC-5, SCK wrote:
Hi

 I try to connect to my database with GWT RPC. But i get a error.

Help please

Thks



java.lang.NullPointerException
        at
org.etude.gwt.server.DatabaseServiceImpl.saveData(DatabaseServiceImpl.java:
37)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at
com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:
569)
        at
com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:
208)
        at
com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:
248)
        at
com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:
62)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
        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:
729)
        at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:
405)
        at
org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:
152)
        at
org.mortbay.jetty.handler.RequestLogHandler.handle(RequestLogHandler.java:
49)
        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.content(HttpConnection.java:843)
        at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:647)
        at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:205)
        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)





package org.etude.gwt.server;

import java.net.URLDecoder;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.Map;

import org.etude.gwt.client.DatabaseService;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class DatabaseServiceImpl extends RemoteServiceServlet
implements
                DatabaseService {

        /**
         *
         */
        private static final long serialVersionUID = 1L;

        @SuppressWarnings("rawtypes")
        @Override
        public void saveData(Map formData) throws IllegalArgumentException {

                try{
                        Class.forName("com.mysql.jdbc.Driver");
                        Connection conn = DriverManager.getConnection("jdbc:mysql://
localhost:3306/oracle", "root", "");

                        Statement st = conn.createStatement();

                        //avec décodage
                        StringBuffer sqlQuery = new StringBuffer("insert into
gwtusers(username,password,email,phone)");
                        sqlQuery.append("values(?,?,?,?)");

                        PreparedStatement stmt =
conn.prepareStatement(sqlQuery.toString());
        LINE 37
stmt.setString(1,URLDecoder.decode(formData.get("username").toString(),"UTF-8"));
        
stmt.setString(2,URLDecoder.decode(formData.get("password").toString(),"UTF-8"));
        
stmt.setString(3,URLDecoder.decode(formData.get("email").toString(),"UTF-8"));
        
stmt.setString(4,URLDecoder.decode(formData.get("phone").toString(),"UTF-8"));
                        stmt.execute();

                        //sans décodage
                        st.executeQuery("insert into gwtusers values
('"+formData.get("username").toString()+
                                        "','"+formData.get("password").toString()+
                                        "','"+formData.get("email").toString()+
                                        "','"+formData.get("phone").toString()+"')");

                        conn.close();
                        //stmt.close();
                        //st.close();


                }catch(Exception e){
                        e.printStackTrace();
                }

        }

}

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-web-toolkit/-/0VtKEmaj1bEJ.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment