java NullPointerException when trying to draw - java

I'm trying to draw a picture based on a random array of numbers. So far I've had partial success, as the background color (white) does come up.
However, the picture does not draw. Instead all I see is the white background and this error:
PixelAvatarGen
XphnX
Black and white or 8-bit color?
0 = B&W, 1 = 8bit
1
Print name to make into avatar.
2
5:2:7:0:3:6:7:0:6:7:7:3:1:4:0:6:5:1:7:4:0:0:5:6:6:3:0:2:3:7:2:4:7:5:4:2:3:6:0:1:4:1:6:4:1:1:1:5:7:5:1:5:4:0:4:5:1:5:6:0:5:4:6:0:0:6:0:0:0:6:7:5:2:6:4:0:1:1:1:7:0:2:1:4:0:2:3:3:3:3:2:6:6:2:6:4:2:7:5:4:7:3:3:3:1:5:1:6:1:3:4:6:0:1:1:2:5:7:4:1:0:2:4:4:4:6:3:7:7:1:0:6:3:6:3:7:6:5:2:5:5:3:5:3:5:4:4:7:0:5:7:4:2:4:5:7:1:0:3:2:7:7:2:3:5:5:2:7:5:6:6:7:4:3:0:7:4:7:1:0:0:7:4:4:3:7:6:0:4:2:7:6:7:3:2:4:2:7:3:0:5:6:6:7:7:2:2:0:2:5:7:6:5:1:0:6:1:5:2:0:7:0:7:0:4:1:0:3:4:3:7:7:4:4:2:7:0:7:4:7:1:6:1:4:4:2:4:7:3:0:4:4:6:6:3:5:
Drawing11
Exception in thread "AWT-EventQueue-0" Drawing11
java.lang.NullPointerException
at xphnx.pixelgen.Screen.paintComponent(Screen.java:25)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at java.awt.Window.paint(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$700(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(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.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)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at xphnx.pixelgen.Screen.paintComponent(Screen.java:25)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent._paintImmediately(Unknown Source)
at javax.swing.JComponent.paintImmediately(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$700(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(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.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)
This is the complete output of the program. The "1" after 0 = B&W and "2" after "Print name to..." are user input.
Here is the Screen.java:
public class Screen extends JPanel {
String[] pixels;
Color[] proxlol;
public Screen(String[] in1, Color[] in2) {
in1 = pixels;
in2 = proxlol;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.WHITE);
int row = 1, column = 1, row10, column10;
for(int i = 0; i != 256; i++) {
System.out.println("Drawing" + row + column);
g.setColor(proxlol[i]);
if(i == 17 ||
i == 33 ||
i == 49 ||
i == 65 ||
i == 81 ||
i == 97 ||
i == 113 ||
i == 129 ||
i == 145 ||
i == 161 ||
i == 177 ||
i == 193 ||
i == 209 ||
i == 225 ||
i == 241
){
row++;
column = 1;
}
row10 = row*10;
column10 = column*10;
g.fillRect(row10, column10, 10, 10);
System.out.println("Done drawing" + row + column);
column++;
}
}
}
In case needed, Main.java and Arranger.java are on pastebin.
I've got little experience with Swing before. What's going on?

You never assign a value to your class variable proxlol.
public class Screen extends JPanel {
String[] pixels;
Color[] proxlol;
public Screen(String[] in1, Color[] in2) {
in1 = pixels;
in2 = proxlol;
}
}
You assign (to no effect) the uninitialized class variable to the input parameter in2. So, in your paint method when you reference the array of Colors you get an NPE.

Related

How to get JTable size after data change occurs

I've been working on a project to pull data from an API and put it in a JTable. When the JTable is populated with data I'd like for it to save an image of everything within the table, including the headers.
The problem is the data seems to only be saved one time. If I manipulate the table (I.E: use a different JComponent to remove rows) and try saving the image again it gives an error because the height/width cannot be set to 0.
Here is [some of] my code for adding data into the table:
missionTable = new JTable(info, headers);
for (int i = 0; i < missionTable.getColumnCount(); i++) {
missionTable.getColumnModel().getColumn(i).setCellRenderer(new RarityRenderer());
}
int maxSize = 0;
JTextArea label = null;
// Resize rows to fit table values
for (int i = 0; i < info.length; i++) {
maxSize = 0;
if (info[i] != null && info[i][2] != null && info[i][3] != null) {
label = new JTextArea(info[i][2]);
label.setBounds(0, 0, 95, 20);
if (label.getPreferredSize().height > maxSize)
maxSize = label.getPreferredSize().height;
label = new JTextArea(info[i][3]);
label.setBounds(0, 0, 320, 20);
label.setLineWrap(true);
label.setWrapStyleWord(true);
if (label.getPreferredSize().height > maxSize)
maxSize = label.getPreferredSize().height;
}
if (maxSize > 0)
missionTable.setRowHeight(i, maxSize > 40 ? 52 : 40);
}
label = null;
missionScrollPane.setViewportView(missionTable);
System.out.println("H:" + missionTable.getHeight() + " W:" + missionTable.getWidth());
saveTable(missionTable, "./test.jpg");// Line 904 from stacktrace
And here is the saveTable(JTable, String) method:
public void saveTable(JTable table, String filename) {
try {
int ww = Math.max(table.getWidth(), table.getTableHeader().getWidth());
int hh = table.getHeight() + table.getTableHeader().getHeight();
BufferedImage bi = new BufferedImage(ww, hh, BufferedImage.TYPE_INT_RGB);// Line 980 from stacktrace
Graphics2D g2 = bi.createGraphics();
table.getTableHeader().paint(g2);
g2.translate(0, table.getTableHeader().getHeight());
table.paint(g2);
g2.dispose();
OutputStream out = new FileOutputStream(filename);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(bi);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Along with the output + stacktrace:
H:0 W:0
java.lang.IllegalArgumentException: Width (0) and height (0) cannot be <= 0
at java.awt.image.DirectColorModel.createCompatibleWritableRaster(Unknown Source)
at java.awt.image.BufferedImage.<init>(Unknown Source)
at stuff.game.ux.StormUI.saveComponentAsJPEG(StormUI.java:980)
at stuff.game.ux.StormUI.refreshCurrentTable(StormUI.java:904)
at stuff.game.ux.StormUI.changeFilter(StormUI.java:720)
at stuff.game.ux.StormUI$9.mouseClicked(StormUI.java:425)
at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
at java.awt.AWTEventMulticaster.mouseClicked(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)
I've honestly run out of things to try, setting the height/width manually only creates a solid black image.

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;

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

Getting a nullpointerexception error when trying to draw to canvas

I'm trying to paint essentially a checkerboard pattern to the screen by using a 2d array and painting 10x10 pixel blocks at the current coordinate based on what character is read from the array location. I think this is all the code related to the problem:
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g.fillRect(0, 0, this.getWidth(),this.getHeight());
for(int x = 0;x<=3;x++){
for(int y = 0;y<=3;y++){
// NPE occurs on this line:
if (globalmap[x][y] == '1'){g2d.fillRect(10*y, 10*x, 10,10);}
}
}
}
This is the map array:
0000
0011
0100
0000
Stack trace:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.side.side.GameEngine.paint(GameEngine.java:64)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at java.awt.Window.paint(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(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)
Line 64:
if (globalmap[x][y] == '1') { g2d.fillRect(10*y, 10*x, 10,10); }
Likely your globalmap array elements are null, but it's hard to tell based on what you're posting. Note that this line is terrible:
if (globalmap[x][y] == '1'){g2d.fillRect(10*y, 10*x, 10,10);}
You need to spread this out on several lines if only for debugging purposes:
if (globalmap[x][y] == '1') {
g2d.fillRect(10*y, 10*x, 10,10);
}

Categories

Resources