简体   繁体   English

Android Java空指针异常

[英]Android Java Null Pointer Exception

I am trying to connect to a database on a remote server. 我正在尝试连接到远程服务器上的数据库。
I have the following code with private details masked: 我有以下代码,其中隐藏了私人详细信息:


public static void connectToServer () {
        Log.e(tag,"Inside connectToServer");
        String result = "";
        ArrayList toDB = new ArrayList();

        //Assign namevalue pairs to toDB
        try {
            toDB.add(new BasicNameValuePair("A",dta.getA()));
            toDB.add(new BasicNameValuePair("B",dta.getB()));
            toDB.add(new BasicNameValuePair("C",dta.getC()));
            toDB.add(new BasicNameValuePair("D",dta.getD()));
            toDB.add(new BasicNameValuePair("E",dta.getE()));
            toDB.add(new BasicNameValuePair("F",dta.getF()));
        } catch (Exception e) {
            Log.e(tag,e.toString());
        }

        //http post
        try{
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://mydom.com/myFolder/file.php");
                httppost.setEntity(new UrlEncodedFormEntity(toDB));
        }catch(Exception e){
                Log.e(tag, "Error in http connection "+e.toString());
        }
    }

My manifest file is as follows: 我的清单文件如下:
< uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
< uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
< uses-permission android:name="android.permission.INTERNET" />

In logcat I have the following : 在logcat中,我有以下内容:
Inside connectToServer
java.lang.NullPointerException

Why am I getting a NullPointerException ? 为什么会收到NullPointerException?
Thank you. 谢谢。

You are most likely trying to call a method of a non-existent object that you were not successful in creating. 您最有可能尝试调用未成功创建的不存在对象的方法。

Without the exception handler, you would get the line number where the error occurs in the logs. 没有异常处理程序,您将获得日志中发生错误的行号。

您在方法内取消引用的某些对象为null,因此它可能是eta。

Try actually specifying the type of the ArrayList 尝试实际指定ArrayList的类型

for example ArrayList<BasicNameValuePair> toDB = new ArrayList<BasicNameValuePair>(); 例如ArrayList<BasicNameValuePair> toDB = new ArrayList<BasicNameValuePair>();

also make sure tag or dta are actually initialized somewhere (maybe when you delcare them you can go ahead and assign them the empty string. for example: String tag = ""; or DTA dta = new DTA(); or whatever dta is. 还应确保标记或dta实际上已在某处初始化(也许当您对它们进行delcare时,可以继续给它们分配空字符串。例如: String tag = ""; DTA dta = new DTA();或其他dta。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM