Searching data from array in Java [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I have this code, which creates a textfield and a button to let the user search for data in an array:
//Creates a form to search data, list for it and buttons to sort the list.
rightPanel.add(Box.createRigidArea(new Dimension(20, 20)));
final TextField searchTextField;
searchTextField = new TextField ();
rightPanel.add(searchTextField);
rightPanel.add(Box.createRigidArea(new Dimension(20, 20)));
JButton searchConfirmButton = new JButton("Search");
rightPanel.add(searchConfirmButton);
searchConfirmButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
try {
search = searchTextField.getText();
int index = Arrays.asList(data).indexOf(search);
for (String result : data) {
if (result.contains(search)) {
System.out.println(result);
System.out.println(index);
}
}
} catch (NullPointerException e1) {
}
}
});
But whenever I search for the data, whether it exists or not, the program returns me an nullpointerexception with the following content:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at program.Program$4$1.actionPerformed(Program.java:233)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
In this case, row 233 contains this line if (result.contains(search)) {

data looks like an array type that can hold data values or null pointers. You should try:
System.out.println("Found index: " + index);
for (String result : data) {
if (result != null && result.contains(search)) {
System.out.println("Found result: " + result);
}
//If you are still confused than the right else here will help.
}
If, for example, you split a string with two spaces in a row by space, then depending on your function parameters to split(), you can end up with an empty string in that location and you need to skip it effectively.

Related

why this code always enters into catch block?

I am creating a simple Calculator. Whenever i try to perform any operation it enters into catch block. here is code. textFields are as follows:
textfield(to show calculation),textField1(to enter operand),textfield_1(to show Solution)
button = new JButton("+");
button.setForeground(Color.BLACK);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try{
ans=Double.parseDouble(textField1.getText());
s=Double.toString(ans)+"+";
textField.setText(s);
reset();
operand=Double.parseDouble(textField1.getText());
ans+=operand;
textField_1.setText(Double.toString(ans));
}catch(Exception x){
JOptionPane.showMessageDialog(null, "Please Enter a valid argument");
}
}
});
button.setBounds(43, 115, 89, 23);
frame.getContentPane().add(button);
button.setToolTipText("ADDITION");
//reset textField1
public void reset(){
textField1.setText("");
}
//............//
java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at sun.misc.FloatingDecimal.parseDouble(Unknown Source)
at java.lang.Double.parseDouble(Unknown Source)
at gui.Calcu$2.actionPerformed(Calcu.java:92)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Your problem is here:
reset();//this makes the textField1 hold an empty String
operand=Double.parseDouble(textField1.getText());
So when you are trying to read and parse a Double from it you are geting a NumberFormatException because you can not convert an empty String into a Double.
One solution to your problem is to create a second JTextField and let the user write the first number he wants to add to the first textField and the second one to the second.Then, when he presses the button to see the result read each number separately, process them and show the result to the screen(either on one of your two textFileds or you could use a third one to display the result).
On the other hand, if you want to have a single textField to handle the user input, you could declare a variable sum outside of all the methods(make it global) and each time you press the "+" button add the value to sum and when the user is done typing numbers just display its String value to the screen.
Additional information on how the event-driven programming works.
Try below code in your button click listner,
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try{
final double val1 = Double.parseDouble(textField1.getText());
ans = val1;
s = Double.toString(ans) + "+";
textField.setText(s);
reset();
operand = val1;
ans += operand;
textField_1.setText(Double.toString(ans));
}catch(Exception x){
JOptionPane.showMessageDialog(null, "Please Enter a valid argument");
}
}
});

Java - JOptionPane.showInputDialog errors

Hello all I am experiencing issues with my JOptionPane.showInputDialog input field.
The issue i am experiencing that is when the cancel button is selected or the OK button is selected with empty or with categorical i am greeted with an error. Any suggestions.
My first attempt at fixing this i changed the c int into an interger therefore i could use an if statement along with null such as
Integer cInterger = new Integer(c);
if (cInterger.equals(null)){
return;}
Along with
cInterger == null
cInterger !=null
But to no avail
int a = 0; //
int b = 0; //
int c = 0; //
if (selected) {
if (command.equals("amount")) {
Scanner readFile = null;
try {
readFile = new Scanner(new FileReader("BANK.txt"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String account = "";
account = readFile.nextLine();
String amount = JOptionPane.showInputDialog(frame,"Enter an amount"); //assign user input to string amount
a= Integer.parseInt(account); //conversion of scanned string to int for easy subtraction
b= Integer.parseInt(amount);
c = a-b;
try {
if( c == 0){
JOptionPane.showMessageDialog(null,"Granted.","Granted",JOptionPane.WARNING_MESSAGE);}
else if (c > 0){
JOptionPane.showMessageDialog(null,"Granted.","Granted",JOptionPane.INFORMATION_MESSAGE); }
else{
JOptionPane.showMessageDialog(null,"Denied.","Denied",JOptionPane.ERROR_MESSAGE);}
Resulting errors
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: null
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at Options$1MyItemListener.itemStateChanged(Options.java:58)
at javax.swing.AbstractButton.fireItemStateChanged(Unknown Source)
at javax.swing.AbstractButton$Handler.itemStateChanged(Unknown Source)
at javax.swing.DefaultButtonModel.fireItemStateChanged(Unknown Source)
at javax.swing.JToggleButton$ToggleButtonModel.setSelected(Unknown Source)
at javax.swing.ButtonGroup.setSelected(Unknown Source)
at javax.swing.JToggleButton$ToggleButtonModel.setSelected(Unknown Source)
at javax.swing.JToggleButton$ToggleButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
If you take a look at the JavaDocs, you will find that
Returns: user's input, or null meaning the user canceled the input
So attempting to parse null makes no sense, instead you should be using something like...
if (amount != null) {
a = Integer.parseInt(amount);
//...
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "" seems reasonably obvious.
if (amount != null) {
if (amount.trim().length() > 0) {
//...
} else {
JOptionPane.showMessageDialog(frame, "The amount you entered is invalid");
}
You should also check the account value as well

Convert vertex label value of the jgraph to array of strings

Basically I have this code to print the cell label of each vertex I click of the jgraph. I am trying to store the values of the cells into a string array. I have tried this:
graphComponent.getGraphControl().addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
ArrayList<Object> objarr = new ArrayList<Object>() ;
if (e.getButton() == 3 && e.getClickCount() == 1) {
long x = e.getX();
long y = e.getY();
Object cell = graphComponent.getCellAt((int) x, (int)y);
System.out.println(graph.convertValueToString(cell));
objarr.add(cell);
}
String[] stringArray = objarr.toArray(new String[100]) ;
}
});
}
I get these errors when i try to click a vertex:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayStoreException
at java.lang.System.arraycopy(Native Method)
at java.util.Arrays.copyOf(Unknown Source)
at java.util.ArrayList.toArray(Unknown Source)
at GUIquery$2.mousePressed(GUIquery.java:498)
at java.awt.AWTEventMulticaster.mousePressed(Unknown Source)
at java.awt.AWTEventMulticaster.mousePressed(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Here is the process to resolve such a problem.
at java.util.ArrayList.toArray(Unknown Source)
at GUIquery$2.mousePressed(GUIquery.java:498)`
Line 498 must be
String[] stringArray = objarr.toArray(new String[100]) ;
Check ArrayList documentation for <T> T[] toArray(T[] a)
When called this tries to store ArrayList<Object> members of type Object in a String[]. So it gave an ArrayStoreException. The root of the problem is that the computer has no idea what type cell is. You declared is as an Object, so that's its type. If getCellAt() returns strings, use String cell.
As a note that is not an elegant solution here, if you had an Object obj_str that you knew was a String, you could cast it with
String str = (String)obj_str;

NullPointerException I cannot resolve

This is the method called when the player has to be removed from the game. Both these methods are in different classes.
GameboardGUI class
Vector<Player> players = new Vector<Player>();
public void removePlayerFromGame(Player playerToRemove)
{
//go through the playerToRemove's properties and reset all their variables
for(int i = 0; i < playerToRemove.getPropertiesOwned().size(); i++)
{
//code to reset the player's properties to an unowned/unmodified state
}
//same with transports
for(int i = 0; i < playerToRemove.getTransportsOwned().size(); i++)
{
//code to reset the player's transports to an unowned/unmodified state
}
//just updating the vector based on the playerToRemove's position
if(players.get(0) == playerToRemove)
{
players.remove(playerToRemove);
updatePlayerInformation();
}
else
{
players.remove(playerToRemove);
updatePlayerVector(players);
updatePlayerInformation();
}
}
This is how the method is called:
If the current player (fromMe) lands on a property and cant afford to pay the rent (i.e. their balance reaches 0 as a result of takefrombalance(1200);, currently hardcoded to 1200 to make testing easier) they are removed in the if statement if(fromMe.isBankrupt())
Property Class
GameboardGUI gui = new GameboardGUI();
public void payRent(Player fromMe, Player toYou, int rent)
{
//remove rent from the current player
fromMe.takeFromBalance(1200);
//add it to the owner
toYou.addToBalance(rent);
GameboardGUI.addGameFeedMessage(fromMe.getName() + " has paid " + rent + " in rent to " + toYou.getName());
GameboardGUI.addGameFeedMessage(toYou.getName() + "'s balance is now " + toYou.getBalance());
if(fromMe.isBankrupt())
{
//THIS IS THE CALL THAT THROWS THE NullPointerException
gui.removePlayerFromGame(fromMe);
}
else
{
GameboardGUI.addGameFeedMessage(fromMe.getName() + "'s balance is now " + fromMe.getBalance());
}
}
Here is the stack trace when the line is reached:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at PropertyTile.payRent(PropertyTile.java:254)
at PropertyTile.landedOnProperty(PropertyTile.java:239)
at GameboardGUI.playerHasLanded(GameboardGUI.java:1905)
at Player.setPosition(Player.java:82)
at Player.movePlayer(Player.java:101)
at GameboardGUI$11.actionPerformed(GameboardGUI.java:1536)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
It's really hard to help you without a stacktrace and the line numbers for your code, but we're trying.
If that's really the line (and not actually within removePlayerFromGame), the member gui hasn't been initialized.
Of course, if it's not that line, it could simply be that playerToRemove is null, and the NullPointerException is actually happening a couple of lines above.
Or, another possibility is that it's happening within removePlayerFromGame, in which case the most likely case is that playerToRemove hasn't been properly set up - that its getPropertiesOwned() or getTransportsOwned() methods are returning null.
I would add the following two lines, as a first step to see what happens:
if(fromMe==null)
System.out.println("from me is null");
if(gui == null)
System.out.println("gui is null");
gui.removePlayerFromGame(fromMe);
One of the two object references is null causing the exception.
Otherwise, the method removePlayerFromGame is doing something, but I don't think that would be the case because the stacktrace would include additional methods.
You say in the code that the Exception is thrown when you reach the line
gui.removePlayerFromGame(fromMe);
However, the top of stacktrace you are showing is Player.movePlayer(), with its bottom at PropertyTile.payRent().
Is this payRent() receiving any parameter? Can you post it source?

How to get the selected row from table model?

How to get the selected row from table model?
I tried the following....
checkBoxes[t].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int index = -1;
for (int i = 0; i < checkBoxes.length; i++) {
if (checkBoxes[i] == e.getSource()) {
String Status=null ;
DbUtility ViewAbsenties=new DbUtility();
ViewAbsenties.loadDriver();
ViewAbsenties.connect();
TableModel tm;
tm = table3.getModel();
if (checkBoxes[i].isSelected() == true) {
Status =(String) tm.getValueAt(i,8);
System.out.println("Status : " + Status );
}
}
}
}
}
when i run the above code, iam getting the following error.
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1 >= 1
at java.util.Vector.elementAt(Unknown Source)
at javax.swing.table.DefaultTableModel.getValueAt(Unknown Source)
at EmployeeLeave$10$1.actionPerformed(EmployeeLeave.java:490)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.JToggleButton$ToggleButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
there are three potential issues
JTables view could be Sorted or Filtered then you have to convertRowIndexToModel
Array(s) started with zero (getValue(0, 0) is 1st. row from 1st. column)
have to set javax.swing.ListSelectionModel properly or determine all selected rows
for better help sooner post an SSCCE

Categories

Resources