0

I got following code and there is this error (I tried to keep the code as short as possible, ignore the getColumnCount etc. functions just the constructor):

The following code is used to make a JTable in Swing by an SQLite statement, I need the booleans for checkboxes (Yes I know I have to edit/add an function, but I wanted to keep the code as small as possible).

Code:

package view;

import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.table.AbstractTableModel;

import controller.Database;

class Test extends AbstractTableModel {
        Database db = new Database();
        ResultSet rs;

        private String[] columnNames = {"Vorname", "Nachname", "E-Mail", "Anrede", "Jahrgang", "Ablösung", "Scheibe", "Waffe", "Gruppe", "Verpflegung", "Aktiv"};
        Object[][] data;

        public Test(){
            int result = 0;
            try {
                rs = db.stat.executeQuery("select count(*) as schuetzencount from schuetze;");
                result = Integer.parseInt(rs.getString("schuetzencount"));
                data = new String[result][11];
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            try {
                rs = db.stat.executeQuery("select * from schuetze as s join waffe as w on w.Waffe_ID = s.Waffe_ID join gruppe as g on g.Gruppe_ID = s.Gruppe_ID join anrede as a on a.Anrede_ID = s.Anrede_ID join verpflegung as v on v.Verpflegung_ID = s.Verpflegung_ID;");
                int counter = 0;
                while(rs.next()){

                    data[counter][1] = rs.getString("Schuetze_Nachname");
                    data[counter][0] = rs.getString("Schuetze_Vorname");
                    data[counter][4] = rs.getString("Schuetze_Jahrgang");
                    data[counter][2] = rs.getString("Schuetze_Email");
                    data[counter][5] = rs.getString("Schuetze_Abloesung");
                    data[counter][6] = rs.getString("Schuetze_Scheibe");
                    data[counter][7] = rs.getString("Waffe_Name");
                    data[counter][8] = rs.getString("Gruppe_Name");
                    data[counter][3] = rs.getString("Anrede_Name");
                    data[counter][9] = rs.getString("Verpflegung_Name");
                    data[counter][10] = true;
                    counter++;
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

        @Override
        public int getColumnCount() {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public int getRowCount() {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public Object getValueAt(int arg0, int arg1) {
            // TODO Auto-generated method stub
            return null;
        }

        public static void main(String[] args) {
            Test t = new Test();
        }
    }

Error:

Exception in thread "main" java.lang.ArrayStoreException: java.lang.Boolean
at view.Test.<init>(Test.java:43)
at view.Test.main(Test.java:72)

If I'll do

Object[][] data = {{"Test", "Test","Test","Test","Test","Test","Test","Test","Test","Test",true}}

it works, but that's not what I need. Then I tried to do a Object[] and fill the Booleans in and then add the Object[] into the data[][] but this also didn't work.

I hope someone can help me, thanks.

Greetz.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
faebuk
  • 51
  • 2
  • 8
  • 1
    Swing? SQL? Please trim down your question to what is necessary to reproduce your problem. –  Jul 30 '13 at 11:48
  • You're trying to store a boolean into a String array element. Just because you declare the variable as type Object, doesn't mean you can do this. If you construct a new Object[...][...] array, then `true` will be automatically converted to a Boolean object. – Adrian Pronk Jul 30 '13 at 11:55

5 Answers5

5

You have Array of Strings and try to put Boolean there in line

data[counter][10] = true;

That is not allowed.

When You do

Object[][] data = {{"Test", "Test","Test","Test","Test","Test","Test","Test","Test","Test",true}}

Java creates for you array of Objects Like:

Object[][] o = new Object[1][6];
o[0][2] = true; // it works
nkukhar
  • 1,975
  • 2
  • 18
  • 37
1

Your array expects a String as dataType, on the other hand are you trying to put a boolean into it.

data[counter][10] = true;

a simple solution to this is using a string "true"instead of the primitive booleantype (or parse it to string)

data[counter][10] = "true";
Joris W
  • 517
  • 3
  • 16
0

You cannot insert a boolean value to an array of String. That is the problem :

data = new String[result][11];
data[counter][10] = true;

At least insert it as a String and parse it when neccessary.

ClaireG
  • 1,244
  • 2
  • 11
  • 23
0

You have defined data as matrix of Objects, but instantiated it as matrix of String. That's why, at runtime, you have this type mismatch exception.

Either instantiate it like:

Object[][] o = new Object[1][6];

or replace boolean value with string:

data[counter][10] = "true";
antonv
  • 1,227
  • 13
  • 21
0

The problem is here:

data = new String[result][11];

You declare the array of String and you do this

data[counter][10] = true;

You have two options:

  1. declare data as

    new Object[result][11];

  2. put string instead of boolean

data[counter][10] = Boolean.TRUE.toString();

Community
  • 1
  • 1
  • Oh thanks, so a little mistake and I search now about more than 1 hours for this problem didn't see it I tought I made new Object[][]. Thanks for the help, works now fine. – faebuk Jul 30 '13 at 11:59