NullPointerException: lock == null

11,765

Solution 1

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.

Solution 2

In addition to the other issue pointed out, notice that you are trying to update your TextView from a new Thread. Android's UI system is not thread safe, so the following line is another problem:

tv1.setText("Response : " + Risposta);'

You can solve this with this example from the docs, without too much restructuring of your code. It uses the Handler class to get back to the UI thread.

Share:
11,765

Related videos on Youtube

Eulante
Author by

Eulante

Updated on June 04, 2022

Comments

  • Eulante
    Eulante almost 2 years

    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?

  • ρяσѕρєя K
    ρяσѕρєя K about 11 years
    +1vote this is also an issue with current code but in this case OP will get CalledFromWrongThreadException: Only the original thread.. instead of NullPointerException
  • Eulante
    Eulante about 11 years
    I've implemented AsyncTask right now and with your tips I've solved my problem. Thank you very much :)