我收到错误:Android java.lang.NullPointerException:println需要一条消息

时间:2015-09-15 06:43:43

标签: java android

我需要帮助才能找到有关我的Android应用的解决方案。尝试登录应用程序后,我收到以下错误。应用程序停止运行(强制停止)。 Fyi,我是开发Android应用程序的新手。

09-15 00:31:42.602: E/AndroidRuntime(2755): FATAL EXCEPTION: main
09-15 00:31:42.602: E/AndroidRuntime(2755): Process: com.mobile.learning, PID: 2755
09-15 00:31:42.602: E/AndroidRuntime(2755): java.lang.NullPointerException: println needs a message
09-15 00:31:42.602: E/AndroidRuntime(2755):     at android.util.Log.println_native(Native Method)
09-15 00:31:42.602: E/AndroidRuntime(2755):     at android.util.Log.d(Log.java:139)
09-15 00:31:42.602: E/AndroidRuntime(2755):     at com.mobile.learning.login$1.onClick(login.java:61)
09-15 00:31:42.602: E/AndroidRuntime(2755):     at android.view.View.performClick(View.java:4756)
09-15 00:31:42.602: E/AndroidRuntime(2755):     at android.view.View$PerformClick.run(View.java:19749)
09-15 00:31:42.602: E/AndroidRuntime(2755):     at android.os.Handler.handleCallback(Handler.java:739)
09-15 00:31:42.602: E/AndroidRuntime(2755):     at android.os.Handler.dispatchMessage(Handler.java:95)
09-15 00:31:42.602: E/AndroidRuntime(2755):     at android.os.Looper.loop(Looper.java:135)
09-15 00:31:42.602: E/AndroidRuntime(2755):     at android.app.ActivityThread.main(ActivityThread.java:5221)
09-15 00:31:42.602: E/AndroidRuntime(2755):     at java.lang.reflect.Method.invoke(Native Method)
09-15 00:31:42.602: E/AndroidRuntime(2755):     at java.lang.reflect.Method.invoke(Method.java:372)
09-15 00:31:42.602: E/AndroidRuntime(2755):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
09-15 00:31:42.602: E/AndroidRuntime(2755):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

这是我的login.java:

package com.mobile.learning;

import mobile.config.*;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
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 login extends Activity {
    public koneksi linkurl;
    String SERVER_URL;
    private Button login;
    private EditText username, password;
    public ProgressDialog progressDialog;

    //private TextView notif;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.signin);

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
        StrictMode.setThreadPolicy(policy);

        login = (Button) findViewById(R.id.login);
        username = (EditText) findViewById(R.id.uname);
        password = (EditText) findViewById(R.id.passwd);

        //final TextView notif = (TextView) findViewById(R.id.tv_error);

        login.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                // TODO Auto-generated method stub

                String Re;
                String mUsername = username.getText().toString();
                String mPassword = password.getText().toString();

                Re=tryLogin(mUsername, mPassword);

                Log.d("Check","Here");
                Log.d("Re",Re);
                String temp_check=Re.trim();
                if(temp_check.equals("1"))
                {
                     String nama = username.getText().toString();
                     Intent newIntent = new Intent(login.this, halamanUtama.class);
                     String txtnama = String.valueOf(nama);

                     //membuat Bundle
                     Bundle bundle = new Bundle();

                     //menentukan parameter Bundle (id,isi) --> id=nama dan isinya adalah variabel dari txtnama
                     bundle.putString("nama", txtnama);

                     //menambahkan bundle pada intent
                     newIntent.putExtras(bundle);
                     startActivityForResult(newIntent, 0);
                    //notif.setText("SUKSES");
                }
                else
                {
                    createDialog("Maaf", "Username Atau Password Salah !");                 
                }               
            }
        });
    }
    protected String tryLogin(String mUsername, String mPassword)
    {           
      Log.d(" TryLoginCheck ","Here");
        HttpURLConnection connection;
       OutputStreamWriter request = null;

            URL url = null;   
            String response = null;   
            String temp=null;
            String parameters = "username="+mUsername+"&password="+mPassword;   
            System.out.println("UserName"+mUsername+"\n"+"password"+mPassword);
            Log.d("Parameters",parameters);
            try
            {
                ;
                linkurl = new koneksi("/login.php");
                SERVER_URL = linkurl.getUrl();
                //"http://182.6.232.37:80/ta/login.php"
                url = new URL(SERVER_URL);
                connection = (HttpURLConnection) url.openConnection();
                connection.setDoOutput(true);
                connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                connection.setRequestMethod("POST");    

                request = new OutputStreamWriter(connection.getOutputStream());
                request.write(parameters);
                request.flush();
                request.close();            
                String line = "";               
                InputStreamReader isr = new InputStreamReader(connection.getInputStream());
                BufferedReader reader = new BufferedReader(isr);
                StringBuilder sb = new StringBuilder();
                while ((line = reader.readLine()) != null)
                {

                    sb.append(line + "\n");
                }
                temp=sb.toString();
                Log.d("Temp",temp);
                // Response from server after login process will be stored in response variable.                
                response = sb.toString();
                Log.d("Response",response);
               Log.d("Sb Value",sb.toString());
                isr.close();
                reader.close();


            }
            catch(IOException e)
            {
                Toast.makeText(this,e.toString(),0).show();
            }
           // Log.d("Response",response);
            return response;
    }

    private void createDialog(String title, String text) {
        AlertDialog ad = new AlertDialog.Builder(this)
        .setPositiveButton("Ok", null)
        .setTitle(title)
        .setMessage(text)
        .create();
        ad.show();
    }

}

2 个答案:

答案 0 :(得分:1)

此代码导致的错误:Log.d(“Re”,Re); ,我认为由tryLogin函数引起的错误/异常导致Re == null。也许你需要在tryLogin中检查你的代码。或最简单的解决方案改变:

String response = null;
String temp=null;

String response = ""; //or anything string in here String temp=null;

答案 1 :(得分:0)

看起来Re为空。如果你在tryLogin()中得到异常,就会发生这种情况。

这就是Log.d("Re",Re);引发这种情况的方式。 为什么你试试Log.d("Re",null);

检查Re是否为null bevore如果在tryLogin()中出现异常,则记录或返回空字符串。