android:空指针异常

时间:2013-03-20 04:37:23

标签: android

我一直在开发一个应用程序,公平的预测是它的一部分..它也包含一个webpart ..但我无法登录到那个部分..应用程序终止到那个时间..登录页面链接到一个jsp页面并转入一个mysql数据库..我在这里发布代码..请帮助我..提前感谢..

Tlogin.java

package com.example.tracemeendpointsjson;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class TLogin extends Activity  {

    EditText uname,pwd;
    Button log,signup;
    String response="";
    NameValuePair v;
    String LoginID,LoginPassword;
    HttpRequestClass mynetworkclass;
    ArrayList<NameValuePair> namevaluepair;

    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tlogin);
        uname=(EditText) findViewById(R.id.uname);
        pwd=(EditText) findViewById(R.id.pwd);
        log=(Button) findViewById(R.id.login);
        signup=(Button) findViewById(R.id.bSignup);
        log.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                LoginID=uname.getText().toString();
                LoginPassword=pwd.getText().toString();
                //CommonClassForLogin q=(CommonClassForLogin) getApplication();
                //q.myUID=LoginID;

                String url="http://"+details.ip+"taxinew/mobile/login.jsp";

            new AsyncTaskLogin().execute(url,LoginID,LoginPassword);


            }
        });

    }
    public class AsyncTaskLogin extends AsyncTask<String, Integer, String>  
    {

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
            mynetworkclass=new HttpRequestClass();
            namevaluepair=new ArrayList<NameValuePair>();
            namevaluepair.add(new BasicNameValuePair("username",params[1]));
            namevaluepair.add(new BasicNameValuePair("password",params[2]));

            try {
                String result=mynetworkclass.getinformation(params[0],namevaluepair);
                Log.d("dsfwewerte", result);
                result=result.replaceAll("\\r|\\n", "");
            return result;

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

        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            //Log.d("222222222", result);

            if(result.equals("0"))
            {
                Toast.makeText(getApplicationContext(),"Welcomes You", Toast.LENGTH_SHORT).show();
                Intent in=new Intent(getApplicationContext(), Trip.class);
                startActivity(in);

                //Log.d("11111111111", result);
            }
            else
            {
                Toast.makeText(getApplicationContext(),"Wrong Entry", Toast.LENGTH_SHORT).show();
                Intent in1=new Intent(getApplicationContext(), Home.class);
                startActivity(in1);

                //Log.d("0000000000", result);

            }




        }



    }

}

HttprequestClass如下

package com.example.tracemeendpointsjson;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;
import android.widget.Toast;

public class HttpRequestClass {
    HttpClient httpclient;
    public String getinformation(String url,ArrayList<NameValuePair> namevaluepair) throws ParseException, IOException
    {
        httpclient=new DefaultHttpClient();
        HttpPost request=new HttpPost(url);
        request.setEntity(new UrlEncodedFormEntity(namevaluepair));
        try
        {
            HttpResponse respnse=httpclient.execute(request);
            int status=respnse.getStatusLine().getStatusCode();
            if(status==200)
            {
                HttpEntity e=respnse.getEntity();
                String data=EntityUtils.toString(e);
                return data;
            }
        }
        catch(ClientProtocolException e)
        {
            e.printStackTrace();
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }


        return null;

    }


    public String getsingleinformation(String url) throws ParseException, IOException
    {
        httpclient=new DefaultHttpClient();
        HttpPost request=new HttpPost(url);
        try
        {
            HttpResponse respnse=httpclient.execute(request);
            int status=respnse.getStatusLine().getStatusCode();
            if(status==200)
            {
                HttpEntity e=respnse.getEntity();
                String data=EntityUtils.toString(e);
                return data;
            }                               
        }
        catch(ClientProtocolException e)
        {
            e.printStackTrace();
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }


        return null;

    }
}

清单文件如下所示:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.tracemeendpointsjson"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"/>
 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.SEND_SMS"/>
    <uses-permission android:name="android.permission.WAKE_LOCK"/>

    <application
        android:icon="@drawable/traffic"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
           <uses-library android:name="com.google.android.maps"/>
        <activity
            android:name="com.example.tracemeendpointsjson.Home"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MainActivity"></activity>
         <activity
            android:name=".UploadDetails"></activity>
        <activity
            android:name=".CurrentLocation"></activity>
        <activity
            android:name=".GmailLogin"></activity>
         <activity
            android:name=".MailReceivers"></activity>
         <activity
            android:name=".ShowWeather"></activity>
         <activity
            android:name=".DisplayWeather"></activity>
          <activity
            android:name=".Trip"></activity>
           <activity
            android:name=".ShowRoute"></activity>
           <activity
            android:name=".Parking"></activity>
            <activity
            android:name=".Estfare"></activity>
             <activity
            android:name=".GetFare"></activity>
           <activity 
               android:name=".TLogin">
           </activity>
           <activity
            android:name=".ParkingPlaceShow"></activity>

        <service android:name=".ServiceClass" >
        </service>

      <!-- <service android:name=".GPSTracker"></service> -->  

    </application>

</manifest>

logcat输出:

03-20 09:51:30.866: E/AndroidRuntime(476): FATAL EXCEPTION: main
03-20 09:51:30.866: E/AndroidRuntime(476): java.lang.NullPointerException
03-20 09:51:30.866: E/AndroidRuntime(476):  at com.example.tracemeendpointsjson.TLogin$AsyncTaskLogin.onPostExecute(TLogin.java:85)
03-20 09:51:30.866: E/AndroidRuntime(476):  at com.example.tracemeendpointsjson.TLogin$AsyncTaskLogin.onPostExecute(TLogin.java:1)
03-20 09:51:30.866: E/AndroidRuntime(476):  at android.os.AsyncTask.finish(AsyncTask.java:417)
03-20 09:51:30.866: E/AndroidRuntime(476):  at android.os.AsyncTask.access$300(AsyncTask.java:127)
03-20 09:51:30.866: E/AndroidRuntime(476):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
03-20 09:51:30.866: E/AndroidRuntime(476):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-20 09:51:30.866: E/AndroidRuntime(476):  at android.os.Looper.loop(Looper.java:130)
03-20 09:51:30.866: E/AndroidRuntime(476):  at android.app.ActivityThread.main(ActivityThread.java:3683)
03-20 09:51:30.866: E/AndroidRuntime(476):  at java.lang.reflect.Method.invokeNative(Native Method)
03-20 09:51:30.866: E/AndroidRuntime(476):  at java.lang.reflect.Method.invoke(Method.java:507)
03-20 09:51:30.866: E/AndroidRuntime(476):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-20 09:51:30.866: E/AndroidRuntime(476):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-20 09:51:30.866: E/AndroidRuntime(476):  at dalvik.system.NativeStart.main(Native Method)
03-20 09:51:30.896: W/ActivityManager(83):   Force finishing activity com.example.tracemeendpointsjson/.TLogin
03-20 09:51:31.428: W/ActivityManager(83): Activity pause timeout for HistoryRecord{40803618 com.example.tracemeendpointsjson/.TLogin}

03-20 09:51:33.555: E/InputDispatcher(83): channel '40800e70 com.example.tracemeendpointsjson/com.example.tracemeendpointsjson.Home (server)' ~ Consumer closed input channel or an error occurred.  events=0x8
03-20 
09:51:33.555: E/InputDispatcher(83): channel '40800e70 com.example.tracemeendpointsjson/com.example.tracemeendpointsjson.Home (server)' ~ Channel is unrecoverably broken and will be disposed!

Login.jsp如下:

<%-- 
    Document   : login
--%>
<%@page import="javax.swing.JOptionPane,DB.DbCon"%>
<%@page import="org.json.JSONObject" %>
<%@page import="org.json.JSONArray" %>
<%@page import="java.sql.ResultSet"%>


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
String username=request.getParameter("username");
String password=request.getParameter("password");

JSONObject jo= new JSONObject();
//JSONArray ja=new JSONArray();
String query="select * from Userdetails where name='"+username+"' and pwd='"+password+"' ";

DbCon o=new DbCon();
ResultSet rs=o.selects(query);
         if(rs.next()){
             jo.accumulate("0", true);

         }

       else
                     {

        jo.accumulate("1", false);
               }

     out.print(jo);


%>

4 个答案:

答案 0 :(得分:2)

试试这些代码..我认为这是问题所在。

protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        String result;
        mynetworkclass=new HttpRequestClass();
        namevaluepair=new ArrayList<NameValuePair>();
        namevaluepair.add(new BasicNameValuePair("username",params[1]));
        namevaluepair.add(new BasicNameValuePair("password",params[2]));

        try {
            result=mynetworkclass.getinformation(params[0],namevaluepair);
            Log.d("dsfwewerte", result);
            result=result.replaceAll("\\r|\\n", "");
        return result;

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

答案 1 :(得分:1)

您的result为空,对if条件的简单更改可以解决此问题

if("0".equals(result))

答案 2 :(得分:0)

除了null之外,尝试在result方法中返回doInBackground。在该方法结束时,您返回null,这就是它抛出nullpointer excepection的原因。

或者您需要在onPostExecute方法中检查结果的空值,以防止抛出异常。

 @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        mynetworkclass=new HttpRequestClass();
        namevaluepair=new ArrayList<NameValuePair>();
        namevaluepair.add(new BasicNameValuePair("username",params[1]));
        namevaluepair.add(new BasicNameValuePair("password",params[2]));
        try {
            String result=mynetworkclass.getinformation(params[0],namevaluepair);
            Log.d("dsfwewerte", result);
            result=result.replaceAll("\\r|\\n", "");
        return result;
    } catch (Exception e) {
        e.printStackTrace();
    }
        return result;       <----- Check this line.
    }
    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
      //Check for the result is null or not.
       if(result != null)
       {
             if(result.equals("0"))
             {}
        }
   }

答案 3 :(得分:0)

您可以使用result返回"with No data" default并与之匹配,您也可以调试并检查您的回复内容