Results 1 to 2 of 2
  1. #1
    boyfunky is offline Member
    Join Date
    Jun 2014
    Posts
    5
    Rep Power
    0

    Default Gettign Data from Database, Posting it as a ListView in another Activity

    Java Code:
    public class Dbconnect extends ListActivity {

    static String names [] ;

    public static void main (String [] args)throws InstantiationException, IllegalAccessException, ClassNotFoundException{
    String connString = "jdbc:jtds:sqlserver://DRLOVE/EDMS;instance=MSSQLSERVER;";
    String query = "select * from Employee";
    String driver = "net.sourceforge.jtds.jdbc.Driver";
    Connection conn = null;
    ResultSet reset = null;
    try{
    Class.forName(driver).newInstance();
    conn = DriverManager.getConnection(connString);
    if(!conn.isClosed()){
    //add appropriate logging here to track progress
    Statement stmt = conn.createStatement();
    reset = stmt.executeQuery(query);

    //names = reset.getArray(2);
    //names = reset.toString()
    int i = 0;
    names = new String[reset.getMetaData().getColumnCount()];
    while(reset.next()){
    //System.out.println(reset.getString(2));
    names[i] = reset.getString(2);
    i++;
    }
    reset.close();
    stmt.close();
    }
    }
    catch (SQLException e) {
    e.printStackTrace();
    } catch (InstantiationException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IllegalAccessException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setListAdapter(new ArrayAdapter<String>(Dbconnect.this, android.R.layout.simple_list_item_1, names));
    }
    }
    2nd Java Activity page
    Java Code:
    public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle RunBackground) {
    // TODO Auto-generated method stub
    super.onCreate(RunBackground);
    setContentView(R.layout.background);
    Thread timer = new Thread(){
    public void run(){
    try {
    sleep(5000);
    } catch (InterruptedException e){
    e.printStackTrace();
    }finally {
    /* opens new activity and starts activity*/
    Intent openStartingPoint = new Intent("com.example.graphmobile.DBCONNECT");
    startActivity(openStartingPoint);

    }
    }
    };
    timer.start();
    }

    @Override
    protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    finish();
    }
    }
    This program should load the MainActivity class, load d background then start d DbConnect activity. but I get an error when it switched to d DbConnect. This is the wrror i am getting

    07-04 05:29:40.856: E/AndroidRuntime(2034): FATAL EXCEPTION: main
    07-04 05:29:40.856: E/AndroidRuntime(2034): Process: com.example.graphmobile, PID: 2034
    07-04 05:29:40.856: E/AndroidRuntime(2034): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.graphmobile/com.example.graphmobile.Dbconnect}: java.lang.NullPointerException: storage == null
    07-04 05:29:40.856: E/AndroidRuntime(2034): at android.app.ActivityThread.performLaunchActivity(A ctivityThread.java:2195)
    07-04 05:29:40.856: E/AndroidRuntime(2034): at android.app.ActivityThread.handleLaunchActivity(Ac tivityThread.java:2245)
    07-04 05:29:40.856: E/AndroidRuntime(2034): at android.app.ActivityThread.access$800(ActivityThre ad.java:135)
    07-04 05:29:40.856: E/AndroidRuntime(2034): at android.app.ActivityThread$H.handleMessage(Activit yThread.java:1196)
    07-04 05:29:40.856: E/AndroidRuntime(2034): at android.os.Handler.dispatchMessage(Handler.java:10 2)
    07-04 05:29:40.856: E/AndroidRuntime(2034): at android.os.Looper.loop(Looper.java:136)
    07-04 05:29:40.856: E/AndroidRuntime(2034): at android.app.ActivityThread.main(ActivityThread.jav a:5017)
    07-04 05:29:40.856: E/AndroidRuntime(2034): at java.lang.reflect.Method.invokeNative(Native Method)
    07-04 05:29:40.856: E/AndroidRuntime(2034): at java.lang.reflect.Method.invoke(Method.java:515)
    07-04 05:29:40.856: E/AndroidRuntime(2034): at com.android.internal.os.ZygoteInit$MethodAndArgsCa ller.run(ZygoteInit.java:779)
    07-04 05:29:40.856: E/AndroidRuntime(2034): at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:595)
    07-04 05:29:40.856: E/AndroidRuntime(2034): at dalvik.system.NativeStart.main(Native Method)
    07-04 05:29:40.856: E/AndroidRuntime(2034): Caused by: java.lang.NullPointerException: storage == null
    07-04 05:29:40.856: E/AndroidRuntime(2034): at java.util.Arrays$ArrayList.<init>(Arrays.java:38)
    07-04 05:29:40.856: E/AndroidRuntime(2034): at java.util.Arrays.asList(Arrays.java:155)
    07-04 05:29:40.856: E/AndroidRuntime(2034): at android.widget.ArrayAdapter.<init>(ArrayAdapter.ja va:128)
    07-04 05:29:40.856: E/AndroidRuntime(2034): at com.example.graphmobile.Dbconnect.onCreate(Dbconne ct.java:62)
    07-04 05:29:40.856: E/AndroidRuntime(2034): at android.app.Activity.performCreate(Activity.java:5 231)
    07-04 05:29:40.856: E/AndroidRuntime(2034): at android.app.Instrumentation.callActivityOnCreate(I nstrumentation.java:1087)
    07-04 05:29:40.856: E/AndroidRuntime(2034): at android.app.ActivityThread.performLaunchActivity(A ctivityThread.java:2159)
    07-04 05:29:40.856: E/AndroidRuntime(2034): ... 11 more

    What am I doing wrong? I want to read data from a database and put it as a ListView or ListItem.

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Gettign Data from Database, Posting it as a ListView in another Activity

    Closing this thread as a duplicate.
    Norm's answered in the other thread.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Using ListView to Retrieve Data from Database
    By boyfunky in forum Android
    Replies: 7
    Last Post: 07-09-2014, 07:47 PM
  2. Activity's ListView items do not show
    By KernelPanic in forum Android
    Replies: 0
    Last Post: 05-17-2012, 07:21 AM
  3. Java - Database posting issue
    By jason360 in forum New To Java
    Replies: 1
    Last Post: 07-14-2011, 02:12 AM
  4. Posting data to third party web forms and getting results
    By javaGirl42 in forum Advanced Java
    Replies: 1
    Last Post: 04-02-2011, 07:40 AM
  5. Problem with NTLM ,data not posting to the server
    By raviprakash in forum JavaServer Faces (JSF)
    Replies: 3
    Last Post: 01-03-2008, 03:33 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •