0

I can not do this it is showing this error:

Screenshot

I want to declare an array and then use it in while loop to assign a string variable in it.How can i do that?

package com.example.zaina.assignmentdb;

import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class Show extends AppCompatActivity {
    DB d=new DB(this);
    Cursor h;
    int j=0;
    String t;
    String []g;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show);
        d.open();
        h=d.selectInfo();
        h.moveToFirst();
        while (h.moveToNext()){
            t=h.getString(j);
            g[]={t};
//            Toast.makeText(Show.this, t, Toast.LENGTH_SHORT).show();
        }
        ListAdapter listAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,g);
        ListView listView= (ListView) findViewById(R.id.listView);
        listView.setAdapter(listAdapter);
    }
}
Gurwinder Singh
  • 35,652
  • 5
  • 39
  • 62
  • `g[index here] = t`...You might consider using an `ArrayList` so you can dynamically add elements to it without knowing it's length before hand. – brso05 Jan 04 '17 at 18:26
  • 1
    first read this then you will come to know why compiler is complaining http://stackoverflow.com/questions/1200621/how-to-declare-an-array – RamPrakash Jan 04 '17 at 18:27
  • Please learn how to use arrays in java. – Blip Jan 07 '17 at 01:24
  • Seconding RamPrakash. Read that article on how to declare arrays in Java. This will solve your issue. – Michael Dodd Jan 08 '17 at 15:16

3 Answers3

1

I would use ArrayList<String> g = new ArrayList<>(); and then you can just use g.add("Your String")

it will also let you use a for each loop like so

for(String string : g) {Do something}
HTMLlama
  • 1,082
  • 1
  • 8
  • 14
0

You could use an ArrayList and do something like this:

public class Show extends AppCompatActivity {
    DB d=new DB(this);
    Cursor h;
    int j=0;
    String t;
    ArrayList<String> g = new ArrayList<String>();
    String[] g1 = new String[1];
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show);
        d.open();
        h=d.selectInfo();
        h.moveToFirst();
        while (h.moveToNext()){
            t=h.getString(j);
            g.add(t);
//            Toast.makeText(Show.this, t, Toast.LENGTH_SHORT).show();
        }
        g1 = (String [])g.toArray(g1);
        ListAdapter listAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, g1);
        ListView listView= (ListView) findViewById(R.id.listView);
        listView.setAdapter(listAdapter);
    }
}
brso05
  • 12,634
  • 1
  • 17
  • 37
  • I tried your'e code but it is giving error "Java null point exception",can you tell something?@brso05 – Zain Ul Abdin Jan 05 '17 at 12:41
  • it is not showing any line in android monitor,how should i send you a snapshot?@brso05 – Zain Ul Abdin Jan 05 '17 at 14:10
  • Process: com.example.zaina.assignmentdb, PID: 24557 java.lang.NullPointerException at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)this is a small part of error which i am getting @brso05 – Zain Ul Abdin Jan 05 '17 at 14:16
  • @ZainUlAbdin is there anything in the `g1` array? – brso05 Jan 05 '17 at 18:17
  • Look i have a database and i want all the names it had to be shown in the form of list view that's what i am trying to do,so names will be added dynamically.But this code is showing error,i think g1 will have all names because we are assigning it here ' g1=(String[]) g.toArray(g1);'@brso05 – Zain Ul Abdin Jan 06 '17 at 09:00
0

You forgot to initialize your variable g:

String[] g=new String[(Maximum index)]
Tobi
  • 116
  • 4