0

I'm trying to display a list of user names from the database in phpmyadmin in my app ,but I keep getting the following

Unable to start activity ComponentInfo{ie.example.artur.adminapp/ie.example.artur.adminapp.ShowUsers}: java.lang.NullPointerException: storage == null

I understand that this means that the data is not being sent sot the activity cannot start when selected ,the list is empty but my question why is the list not filling up wit the data ?

This is my ShowUser Activity

package ie.example.artur.adminapp;

import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toolbar;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;

import android.view.View;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import static android.R.attr.name;

/**
 * Created by family on 24/07/2017.
 */

public class ShowUsers extends AppCompatActivity {


    ListView lv;
    //String[] names = {"Amy","John","Joseph","Carl"};
    InputStream is = null;
    String line= null;
    String result = null;
    String temp="";
    String[] arr;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.showusers);

        lv = (ListView) findViewById(R.id.lv);

        //Inlfate the list view with the items

        lv.setAdapter(new ArrayAdapter<String>(ShowUsers.this,android.R.layout.simple_list_item_1,arr));

        android.widget.Toolbar toolbar = (android.widget.Toolbar) findViewById(R.id.toolbar);

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

        //set up the code to fetch data from the database


        try {
            HttpClient httpClient = new DefaultHttpClient();

            HttpPost httpPost = new HttpPost("http://10.3.2.51/www/");

            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
            //SETUP THE INPUTSTREAM TO RECEIVE THE DATA (INITIAL)
        }catch (Exception e){
            System.out.println("Exception 1 caught");
        }

        try {

            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
            // Create a String builder object to hold the data
            StringBuilder sb = new StringBuilder();
            while((line = reader.readLine())!=null)
                sb.append(line+"\n");


            //Use the toString() method to get the data in the result

            result = sb.toString();
            is.close();
            //check the data by printing the results in the logcat

            System.out.println("-----Here is my data -----");
            System.out.println(result);

        }catch(Exception e){
            System.out.print("Exception 2 caught");
        }

        try{

            JSONArray jsonArray = new JSONArray(result);//Create a json array
            int count = jsonArray.length();


            for(int i=0; i<count; i++){
                //create a json object to extract the data
                JSONObject json_data = jsonArray.getJSONObject(i);
                temp +=json_data.getString("name")+":";
                //where name is attribute of the getdata table
                //using ':' as the delimiter

            }

            //Afer receiving everything store the contents in a string array from temo separated using the delimiter
            arr = temp.split(":");
            //set the list adapter with the array arr

            lv.setAdapter(new ArrayAdapter<String>(ShowUsers.this,android.R.layout.simple_list_item_1, arr));

        }catch(Exception e){
            System.out.println("I am really bored of writing all these exception blocks");
        }



    }
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        {
            switch (item.getItemId())
            {
                case R.id.action_settings : startActivity (new Intent(this, ShowUsers.class));
                    break;
            }
            return super.onOptionsItemSelected(item);
        }}




}

This is the table users from the database in phpmyadmin :

enter image description here

Also this where I have my tut.php file saved: enter image description here

tut.php :

<?php

$con=mysql_connect("localhost","root","");
mysql_select_db("socialmedia_website",$con);

$r=mysql_query("select name from users where 1";

while($row=mysql_fetch_array($r))

    {

        $out[]=$row;
    }

    print(json_encode($out));
    mysql_close($con)

*Also I'm using an Android device to do this project

This is what I'm getting back : enter image description here

Lucy
  • 65
  • 2
  • 10

3 Answers3

1

You have an error here . you missed ) closing bracket

$r=mysql_query("select name from users where 1");

Note: use mysqli_* functions.

NID
  • 3,238
  • 1
  • 17
  • 28
  • Yes I have fixed that but ,a blank screen simply appears ,its empty :/ – Lucy Jul 27 '17 at 12:17
  • does query returns values? – NID Jul 27 '17 at 12:21
  • i think this might help : https://stackoverflow.com/questions/16777829/java-lang-runtimeexception-unable-to-start-activity-componentinfo-java-lang-nu – NID Jul 27 '17 at 12:22
  • The issue is that I'm not getting back any errors now as well,so I don't know what the issue is now – Lucy Jul 27 '17 at 12:37
  • i'm really sorry i really want to help you more.. but i don't do android.. – NID Jul 27 '17 at 12:38
  • 1
    I think I will choose this as the right answer because ,you solved my first issue which was the the missing `)` ,then I need ask another question – Lucy Jul 27 '17 at 12:40
0
$r=mysql_query("select name from users where 1";

You have something wrong with your query, you missed the closing bracket.

Your query should look like this.

$r=mysql_query("select name from users where 1");
Asim
  • 164
  • 5
0

setAdapter line duplicated two times.so remove first one because that time arr value is null thats why you are getting NullpointerException

 lv = (ListView) findViewById(R.id.lv);
 lv.setAdapter(new ArrayAdapter<String>(ShowUsers.this,android.R.layout.simple_list_item_1,arr)); //remove this
sasikumar
  • 12,540
  • 3
  • 28
  • 48