-2

I'm trying to fetch an array of data from the MSSQL Database into spinner, but I'm unable to do so. I'm getting this exception: Exception due to Array adapter or 'Method invocation spnVnames.setAdapter(NoCoreAdapter) may produce java.lang.NullPointerException' Please help me find my error in the code below.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    currentDate = (TextView) findViewById(R.id.currentDate);
    long date = System.currentTimeMillis();
    SimpleDateFormat sdf = new SimpleDateFormat("  dd/MM/ yyyy");
    String dateString = sdf.format(date);
    currentDate.setText(dateString);
    Spinner spnVnames = (Spinner) findViewById(R.id.spinner);

    String query = " SELECT TaxName FROM tblTax "; //select query

    try {
        Connection con = connectionClass.CONN();
        Statement stmt = con.prepareStatement(query);
        ResultSet rs = stmt.executeQuery(query);
        List < String > data = new ArrayList < > ();

        while (rs.next()) {
            String id = rs.getString("TaxName");
            data.add(id);
        }

        String[] array = data.toArray(new String[data.size()]);
        ArrayAdapter < String > NoCoreAdapter = new ArrayAdapter < > (MainActivity.this, android.R.layout.simple_list_item_1, array);
        spnVnames.setAdapter(NoCoreAdapter);

    } catch (SQLException e) {
        e.printStackTrace();
    }
}
Patrick2607
  • 3,071
  • 1
  • 13
  • 37
NAREN H
  • 11
  • 4
  • 3
    The "...may produce java.lang.NullPointerException" thing is just a warning. If you're actually getting an `Exception` when you run it, you need to post the stack trace. – Mike M. Jun 29 '16 at 06:21
  • FATAL EXCEPTION: main Process: com.example.aits.cherrysales, PID: 23161 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aits.cherrysales/com.example.aits.cherrysales.MainActi‌​vity}: java.lang.NullPointerException ---this was the exception cought...hw to proceed? – NAREN H Jun 29 '16 at 07:02
  • Formatting. Added tag. Improved spelling – Patrick2607 Jun 29 '16 at 07:08

1 Answers1

0

Try this:

ArrayAdapter NoCoreAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item,array);
Batman25663
  • 220
  • 1
  • 3
  • 11