0

I'm developing a Carpool Android app and using Java Servlets and JDBC, the Login and Registration are working fine. After a Custemer login, a listView will be shown with the names and numbers of the drivers, but when i login, it crashs and i didn't understand why and the problem is not in the LoginActivity. the Java Servlet is Ok and the Database is Ok. This is My code :

CustomersActivity:

public class CustomerActivity extends AppCompatActivity {


    private static ListView list_view;
    private static ArrayList<String> ar_list;
    private static String[] tabNum;
    TextView txt;
    String recieved;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custemer);
        list_view = (ListView)findViewById(R.id.list);
        txt = (TextView)findViewById(R.id.txtHello);
        Intent i = getIntent();
        final String name = i.getStringExtra("name");
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    URL url = new URL("http://10.0.2.2:8081/MiniProjectAJ/CustomerServlet");
                    URLConnection cnx = url.openConnection();
                    cnx.setDoOutput(true);
                    OutputStreamWriter out = new OutputStreamWriter(cnx.getOutputStream());
                    out.write("Hello");
                    out.close();
                    BufferedReader bf = new BufferedReader(new InputStreamReader(cnx.getInputStream()));
                    String returnString = "";
                    recieved = "";
                    while ((returnString = bf.readLine()) != null) {
                        recieved = returnString;
                    }
                    String[] sp = recieved.split("\\/");
                    String one = sp[0];
                    int i = Integer.parseInt(one);
                    int p = 1;
                    tabNum = new String[i];
                    while (p <= i) {
                        String two = sp[p];
                        String[] s = two.split("-");
                        String t = s[1];
                        ar_list.add(two);
                        tabNum[p-1] = t;
                        p = p + 1;
                    }
                    bf.close();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            txt.setText("Hello "+name);
                            ArrayAdapter adapter = new ArrayAdapter(CustemerActivity.this, android.R.layout.simple_spinner_dropdown_item, ar_list);
                            list_view.setAdapter(adapter);

                        }
                    });
                }
                catch (MalformedURLException e) {
                    e.printStackTrace();
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        list_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String num = tabNum[position];
                Intent i2 = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+num));
                startActivity(i2);
            }
        });
    }
}

CustomerServlet :

public class CustomerServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public CustomerServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        //doGet(request, response);
        try {
            int length = request.getContentLength();
            byte[] input = new byte[length];
            ServletInputStream sin = request.getInputStream();
            int c, count = 0;
            while ((c=sin.read(input, count, input.length-count))!=-1) {
                count+=c;
            }
            sin.close();
            String receivedString = new String(input);
            System.out.println(receivedString);
            response.setStatus(HttpServletResponse.SC_OK);
            OutputStreamWriter writer = new OutputStreamWriter(response.getOutputStream());
            Connection con = EtablirConnection.getConnection();
            String Q = "SELECT FName,LName,Number FROM drivers;";
            String Q2 = "SELECT COUNT(*) FROM drivers;";
            Statement st = con.createStatement();
            Statement st1 = con.createStatement();
            ResultSet rsc = st1.executeQuery(Q2);
            String send = "";
            while (rsc.next()) {
                send = rsc.getString(1);
            }
            ResultSet rs = st.executeQuery(Q);
            while (rs.next()) {
                String Name = rs.getString("FName")+" "+rs.getString("LName");
                String Number = rs.getString("Number");
                send = send+"/"+Name+"-"+Number;
            }
            System.out.println(send);
            writer.write(send);
            writer.flush();
            writer.close();
        } catch(IOException | SQLException e){
            try {
                response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
                response.getWriter().print(e.getMessage());
                response.getWriter().close();
            } catch (IOException ioe) {
            }
        }
    }
}

Logs :

12-30 20:19:14.129 10056-10056/? I/art: Not late-enabling -Xcheck:jni (already on)
12-30 20:19:14.129 10056-10056/? W/art: Unexpected CPU variant for X86 using defaults: x86
12-30 20:19:14.333 10056-10056/apps.dev.ndroid.projectandroj2ee W/System: ClassLoader referenced unknown path: /data/app/apps.dev.ndroid.projectandroj2ee-1/lib/x86
12-30 20:19:14.393 10056-10056/apps.dev.ndroid.projectandroj2ee I/InstantRun: starting instant run server: is main process
12-30 20:19:14.545 10056-10056/apps.dev.ndroid.projectandroj2ee W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
12-30 20:19:14.828 10056-10085/apps.dev.ndroid.projectandroj2ee I/OpenGLRenderer: Initialized EGL, version 1.4
12-30 20:19:14.828 10056-10085/apps.dev.ndroid.projectandroj2ee D/OpenGLRenderer: Swap behavior 1
12-30 20:19:14.828 10056-10085/apps.dev.ndroid.projectandroj2ee W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
12-30 20:19:14.828 10056-10085/apps.dev.ndroid.projectandroj2ee D/OpenGLRenderer: Swap behavior 0
12-30 20:19:14.932 10056-10056/apps.dev.ndroid.projectandroj2ee W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
12-30 20:19:19.273 10056-10085/apps.dev.ndroid.projectandroj2ee D/OpenGLRenderer: endAllActiveAnimators on 0xaabb1180 (RippleDrawable) with handle 0x9a24d010
12-30 20:19:20.936 10056-10085/apps.dev.ndroid.projectandroj2ee D/OpenGLRenderer: endAllActiveAnimators on 0x9a2d1800 (RippleDrawable) with handle 0x9a24d020
12-30 20:19:35.058 10056-10056/apps.dev.ndroid.projectandroj2ee W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
12-30 20:19:42.830 10056-10512/apps.dev.ndroid.projectandroj2ee D/NetworkSecurityConfig: No Network Security Config specified, using platform default
12-30 20:19:42.912 10056-10515/apps.dev.ndroid.projectandroj2ee E/AndroidRuntime: FATAL EXCEPTION: Thread-5
                                                                              Process: apps.dev.ndroid.projectandroj2ee, PID: 10056
                                                                              java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' on a null object reference
                                                                                  at apps.dev.ndroid.projectandroj2ee.CustemerActivity$1.run(CustemerActivity.java:64)
                                                                                  at java.lang.Thread.run(Thread.java:761)
12-30 20:19:43.084 10056-10056/apps.dev.ndroid.projectandroj2ee W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
12-30 20:19:43.295 10056-10085/apps.dev.ndroid.projectandroj2ee D/OpenGLRenderer: endAllActiveAnimators on 0x9a354800 (RippleDrawable) with handle 0x9a279290
Jalloul95
  • 206
  • 1
  • 2
  • 10

1 Answers1

0

seems like ar_list is not initialized.

ArrayList<String> ar_list = new ArrayList<String>(); something like this
lib4backer
  • 3,337
  • 3
  • 18
  • 16