faebuk
faebuk

Reputation: 51

Java Boolean into Object[][]

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.

Upvotes: 0

Views: 1759

Answers (5)

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();

Upvotes: 0

antonv
antonv

Reputation: 1225

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";

Upvotes: 0

ClaireG
ClaireG

Reputation: 1244

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.

Upvotes: 0

Joris W
Joris W

Reputation: 517

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";

Upvotes: 1

nkukhar
nkukhar

Reputation: 2025

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

Upvotes: 5

Related Questions