NullPointerException异常:锁== NULL [英] NullPointerException: lock == null

查看:284
本文介绍了NullPointerException异常:锁== NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在面临一个问题,在活动中:我想读 Ordinazioni.txt 中的所有文件正确地写在previous活动,使他们在一个的ArrayList ,写它的每个元素到MySQL数据库。

现在,该PHP文件,该文件是正确的,我单独检查它和他们的作品,但在这一点LogCat中块的应用程序。所以,在这里你的活动和LogCat中。

 公共类AggiungiProdotto延伸活动{

    TextView的TV1;
    私有静态字符串indirizzo =htt​​p://10.0.2.2/tesina/Aggiungi_Ordinazione;
    在的FileInputStream = NULL;
    InputStreamReader的InputStreamReader的=新的InputStreamReader(中);
    BufferedReader中的BufferedReader =新的BufferedReader(InputStreamReader的);
    HttpClient的HttpClient的=新DefaultHttpClient();
    HttpPost httppost =新HttpPost(indirizzo);
    StringBuilder的SB =新的StringBuilder();
    INT Tavolo = 1;
    串线;
    公众的ArrayList<字符串> Lettura =新的ArrayList<字符串>();

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.aggiungi_prodotto);
        Invio();
    }

    公共无效Invio(){
        尝试 {
            在的FileInputStream = openFileInput(Ordinazioni.txt);
            而((行= bufferedReader.readLine())!= NULL){
                Lettura.add(线);
            }
        }赶上(FileNotFoundException异常E){
            e.printStackTrace();
        }赶上(IOException异常E){
            // TODO自动生成的catch块
            e.printStackTrace();
        }

        线程线程=新的Thread(){

            @覆盖
            公共无效的run(){
                尝试 {
                    的for(int i = 0; I< Lettura.size();我++){
                        名单<的NameValuePair> Comanda =新的ArrayList<的NameValuePair>(2);
                        Comanda.add(新BasicNameValuePair(诺姆,Lettura.get(ⅰ)));
                        Comanda.add(新BasicNameValuePair(Tavolo
                                Integer.toString(Tavolo).trim()));

                        httppost.setEntity(新UrlEn codedFormEntity(Comanda));

                        ResponseHandler的<字符串> ResponseHandler的=
                                新BasicResponseHandler();
                        最后弦乐Risposta = httpclient.execute(httppost,
                                ResponseHandler所);
                        TV1 =(TextView中)findViewById(R.id.tv1);
                        tv1.setText(回应:+ Risposta);
                    }
                }赶上(ClientProtocolException E){
                    // TODO自动生成的catch块
                    e.printStackTrace();
                }赶上(IOException异常E){
                    // TODO自动生成的catch块
                    e.printStackTrace();
                }
            }
        };

        thread.start();
    }
}
 

LogCat中的错误:

  03-14 18:40:42.028:E / AndroidRuntime(772):致命异常:主要
03-14 18:40:42.028:E / AndroidRuntime(772):java.lang.RuntimeException的:无法实例活动ComponentInfo {com.gabriele.tesina / com.gabriele.tesina.AggiungiProdotto}:显示java.lang.NullPointerException:锁== NULL
03-14 18:40:42.028:E / AndroidRuntime(772):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
03-14 18:40:42.028:E / AndroidRuntime(772):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-14 18:40:42.028:E / AndroidRuntime(772):在android.app.ActivityThread.access $ 600(ActivityThread.java:141)
03-14 18:40:42.028:E / AndroidRuntime(772):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1234)
03-14 18:40:42.028:E / AndroidRuntime(772):在android.os.Handler.dispatchMessage(Handler.java:99)
03-14 18:40:42.028:E / AndroidRuntime(772):在android.os.Looper.loop(Looper.java:137)
03-14 18:40:42.028:E / AndroidRuntime(772):在android.app.ActivityThread.main(ActivityThread.java:5039)
03-14 18:40:42.028:E / AndroidRuntime(772):在java.lang.reflect.Method.invokeNative(本机方法)
03-14 18:40:42.028:E / AndroidRuntime(772):在java.lang.reflect.Method.invoke(Method.java:511)
03-14 18:40:42.028:E / AndroidRuntime(772):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)
03-14 18:40:42.028:E / AndroidRuntime(772):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-14 18:40:42.028:E / AndroidRuntime(772):在dalvik.system.NativeStart.main(本机方法)
03-14 18:40:42.028:E / AndroidRuntime(772):显示java.lang.NullPointerException:产生的原因锁定== NULL
03-14 18:40:42.028:E / AndroidRuntime(772):在java.io.Reader中的< INIT>(Reader.java:64)
03-14 18:40:42.028:E / AndroidRuntime(772):在java.io.InputStreamReader中的< INIT>(InputStreamReader.java:122)
03-14 18:40:42.028:E / AndroidRuntime(772):在java.io.InputStreamReader中的< INIT>(InputStreamReader.java:59)
03-14 18:40:42.028:E / AndroidRuntime(772):在com.gabriele.tesina.AggiungiProdotto< INIT>(AggiungiProdotto.java:32)
03-14 18:40:42.028:E / AndroidRuntime(772):在java.lang.Class.newInstanceImpl(本机方法)
03-14 18:40:42.028:E / AndroidRuntime(772):在java.lang.Class.newInstance(Class.java:1319)
03-14 18:40:42.028:E / AndroidRuntime(772):在android.app.Instrumentation.newActivity(Instrumentation.java:1054)
03-14 18:40:42.028:E / AndroidRuntime(772):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
 

我究竟做错了什么?

解决方案

 的FileInputStream中= NULL;
InputStreamReader的InputStreamReader的=新的InputStreamReader(中);
 

我觉得这里的问题:你添加的的FileInputStream NULL InputStreamReader的

此外,您的code是较为凌乱。看看这里

  TV1 =(TextView中)findViewById(R.id.tv1);
tv1.setText(回应:+ Risposta);
 

您尝试更新的用户界面工作线程因此,如果你将解决第一个问题,下一个问题将是这一点。

您不能从工作线程 UI做的动作。它是不允许的。只有起源(UI)线程可以使操作与界面。

所以,如果你想获得它的工作,你需要使用<一个href="http://developer.android.com/reference/android/app/Activity.html#runOnUiThread%28java.lang.Runnable%29"相对=nofollow> runOnUiThread()已经在UI线程运行正常的线程,而不是。

注意::要提高你的codeI建议您使用的的AsyncTask 执行与互联网的行动,也给一些动作后,在/执行UI更新。 AsyncTask的被指定的东西,像你这样的。这里是好的教程由拉尔斯·沃格尔如果你以前从来没有实现它。

I'm facing a problem in an activity: I want to read all the files from Ordinazioni.txt correctly written in the previous activity, putting them in an ArrayList and write each element of it to a MySQL database.

Now, The PHP file and the file are correct, I've checked it alone and their works, but at this point LogCat blocks the app. So, here you are the activity and the LogCat.

public class AggiungiProdotto extends Activity {

    TextView tv1;
    private static String indirizzo ="http://10.0.2.2/tesina/Aggiungi_Ordinazione";
    FileInputStream in = null;
    InputStreamReader inputStreamReader = new InputStreamReader(in);
    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(indirizzo);
    StringBuilder sb = new StringBuilder();
    int Tavolo = 1;
    String line;
    public ArrayList<String> Lettura = new ArrayList<String>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.aggiungi_prodotto);
        Invio();
    }

    public void Invio() {
        try {
            FileInputStream in = openFileInput("Ordinazioni.txt");
            while ((line = bufferedReader.readLine()) != null) {
                Lettura.add(line);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Thread thread = new Thread() {

            @Override
            public void run() {
                try {
                    for (int i = 0; i < Lettura.size(); i++) {
                        List<NameValuePair> Comanda = new ArrayList<NameValuePair>(2);
                        Comanda.add(new BasicNameValuePair("Nome", Lettura.get(i)));
                        Comanda.add(new BasicNameValuePair("Tavolo",
                                Integer.toString(Tavolo).trim()));

                        httppost.setEntity(new UrlEncodedFormEntity(Comanda));

                        ResponseHandler<String> responseHandler =
                                new BasicResponseHandler();
                        final String Risposta = httpclient.execute(httppost,
                                responseHandler);
                        tv1 = (TextView) findViewById(R.id.tv1);
                        tv1.setText("Response : " + Risposta);
                    }
                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        };

        thread.start();
    }
}

LogCat errors:

03-14 18:40:42.028: E/AndroidRuntime(772): FATAL EXCEPTION: main
03-14 18:40:42.028: E/AndroidRuntime(772): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.gabriele.tesina/com.gabriele.tesina.AggiungiProdotto}: java.lang.NullPointerException: lock == null
03-14 18:40:42.028: E/AndroidRuntime(772):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
03-14 18:40:42.028: E/AndroidRuntime(772):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-14 18:40:42.028: E/AndroidRuntime(772):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-14 18:40:42.028: E/AndroidRuntime(772):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-14 18:40:42.028: E/AndroidRuntime(772):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-14 18:40:42.028: E/AndroidRuntime(772):  at android.os.Looper.loop(Looper.java:137)
03-14 18:40:42.028: E/AndroidRuntime(772):  at android.app.ActivityThread.main(ActivityThread.java:5039)
03-14 18:40:42.028: E/AndroidRuntime(772):  at java.lang.reflect.Method.invokeNative(Native Method)
03-14 18:40:42.028: E/AndroidRuntime(772):  at java.lang.reflect.Method.invoke(Method.java:511)
03-14 18:40:42.028: E/AndroidRuntime(772):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-14 18:40:42.028: E/AndroidRuntime(772):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-14 18:40:42.028: E/AndroidRuntime(772):  at dalvik.system.NativeStart.main(Native Method)
03-14 18:40:42.028: E/AndroidRuntime(772): Caused by: java.lang.NullPointerException: lock == null
03-14 18:40:42.028: E/AndroidRuntime(772):  at java.io.Reader.<init>(Reader.java:64)
03-14 18:40:42.028: E/AndroidRuntime(772):  at java.io.InputStreamReader.<init>(InputStreamReader.java:122)
03-14 18:40:42.028: E/AndroidRuntime(772):  at java.io.InputStreamReader.<init>(InputStreamReader.java:59)
03-14 18:40:42.028: E/AndroidRuntime(772):  at com.gabriele.tesina.AggiungiProdotto.<init>(AggiungiProdotto.java:32)
03-14 18:40:42.028: E/AndroidRuntime(772):  at java.lang.Class.newInstanceImpl(Native Method)
03-14 18:40:42.028: E/AndroidRuntime(772):  at java.lang.Class.newInstance(Class.java:1319)
03-14 18:40:42.028: E/AndroidRuntime(772):  at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
03-14 18:40:42.028: E/AndroidRuntime(772):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)

What am I am doing wrong?

解决方案

FileInputStream in = null;
InputStreamReader inputStreamReader = new InputStreamReader(in);

I think here is problem: you adding FileInputStream that is NULL into InputStreamReader.

Also your code is more messy. Look here

tv1 = (TextView) findViewById(R.id.tv1);
tv1.setText("Response : " + Risposta);

You are trying to update UI from worker Thread so if you will fix first problem, next problem will be this.

You cannot make action with UI from worker Thread. It's not allowed. Only originate(UI) Thread can make actions with UI.

So if you want to get it work you need to use runOnUiThread() that already runs on UI Thread instead of normal Thread.

Note: To improve your code i recommend you use AsyncTask to perform actions with Internet and also to perform UI update during / after some actions. AsyncTask is designated for things like yours. Here is nice tutorial by Lars Vogel if you never implement it before.

这篇关于NullPointerException异常:锁== NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆