First, this is my first time with Apache Derby. I am using netbeans, willing to use embedded apache derby, and I followed the following tutorial for configuring and installing the database
http://netbeans.org/kb/docs/ide/java-db.html#starting
The attached image will show my database status in netbeans
My database name is "contact". Table name is "FRIENDS".
Following is my test code
DatabaseConnector.java
import java.sql.*;
public class DataBaseConnector
{
private Connection con;
public DataBaseConnector()
{
}
private void createConnection()
{
try
{
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
con = DriverManager.getConnection("jdbc:derby:contact","yohan","xyz");
}
catch(Exception e)
{
e.printStackTrace();
}
}
private void closeConnection()
{
try
{
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void insertData(int id, String firstName, String lastName)
{
createConnection();
try
{
PreparedStatement ps = con.prepareStatement("insert into FRIENDS values(?,?,?)");
ps.setInt(1, id);
ps.setString(1, firstName);
ps.setString(2, lastName);
int result = ps.executeUpdate();
if(result>0)
{
System.out.println("Data Inserted");
}
else
{
System.out.println("Something happened");
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
closeConnection();
}
}
}
DatabaseUI.java
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class DatabaseUI extends JFrame
{
private JLabel firstName, id, lastName;
private JTextField idTxt, firstNameTxt, lastNameTxt;
private JButton ok;
public DatabaseUI()
{
firstName = new JLabel("First Name: ");
lastName = new JLabel("Last Name: ");
id = new JLabel("ID: ");
firstNameTxt = new JTextField(10);
lastNameTxt = new JTextField(10);
idTxt = new JTextField(10);
ok = new JButton("OK");
ok.addActionListener(new OKAction());
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new GridLayout(4,2));
centerPanel.add(id);
centerPanel.add(idTxt);
centerPanel.add(firstName);
centerPanel.add(firstNameTxt);
centerPanel.add(lastName);
centerPanel.add(lastNameTxt);
centerPanel.add(new JPanel());
centerPanel.add(ok);
getContentPane().add(centerPanel,"Center");
this.pack();
this.setVisible(true);
}
private class OKAction implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
DataBaseConnector db = new DataBaseConnector();
int id = Integer.parseInt(idTxt.getText());
db.insertData(id, firstNameTxt.getText().trim(), lastNameTxt.getText().trim());
}
}
public static void main(String[]args)
{
new DatabaseUI();
}
}
But, when I am trying to insert data into the database, it is giving me the following error
run:
java.sql.SQLException: Database 'contact' not found.
at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.newSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.handleDBNotFound(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.<init>(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection30.<init>(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection40.<init>(Unknown Source)
at org.apache.derby.jdbc.Driver40.getNewEmbedConnection(Unknown Source)
at org.apache.derby.jdbc.InternalDriver.connect(Unknown Source)
at org.apache.derby.jdbc.AutoloadedDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(DriverManager.java:579)
at java.sql.DriverManager.getConnection(DriverManager.java:221)
at DataBaseConnector.createConnection(DataBaseConnector.java:17)
at DataBaseConnector.insertData(DataBaseConnector.java:40)
at DatabaseUI$OKAction.actionPerformed(DatabaseUI.java:52)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6504)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6269)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4860)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4686)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2713)
at java.awt.Component.dispatchEvent(Component.java:4686)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:680)
at java.awt.EventQueue$4.run(EventQueue.java:678)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Caused by: java.sql.SQLException: Database 'contactDB' not found.
at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown Source)
... 53 more
java.lang.NullPointerException
at DataBaseConnector.insertData(DataBaseConnector.java:43)
at DatabaseUI$OKAction.actionPerformed(DatabaseUI.java:52)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6504)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6269)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4860)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4686)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2713)
at java.awt.Component.dispatchEvent(Component.java:4686)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:680)
at java.awt.EventQueue$4.run(EventQueue.java:678)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
java.lang.NullPointerException
at DataBaseConnector.closeConnection(DataBaseConnector.java:29)
at DataBaseConnector.insertData(DataBaseConnector.java:65)
at DatabaseUI$OKAction.actionPerformed(DatabaseUI.java:52)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6504)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6269)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4860)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4686)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2713)
at java.awt.Component.dispatchEvent(Component.java:4686)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:680)
at java.awt.EventQueue$4.run(EventQueue.java:678)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Please help me. Thanks
The connection URL "jdbc:derby:contact" specifies that the 'contact' database should be found in the current working directory of your process, but that's probably not where it's actually located. When your Java program is run, the current working directory is probably been set somewhere else. You can confirm this by printing out the current working directory at the start of your program.
To get around this for now, you can specify the full path to your database: "jdbc:derby:/path/to/my/db/contact".
If that gets you farther, then you can keep going, but at some point you'll have to think more about where you want your database to be permanently located and how you want to specify that in your program.
Kindly look at your derby/jdbc connection,
This line here:
con = DriverManager.getConnection("jdbc:derby:contact","yohan","xyz");
Check if it looks similar to:
"jdbc:derby://localhost:1527/contact"
Or, as what I've seen, the name of your database is ContactDB... Kindly check...
Thanks...
Related
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
I think, I got an error in this part, but I don't know to repair it. The result of error is
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at bismillahpata.View.hitungHPP1.selanjutnyaActionPerformed(hitungHPP1.java:381)
at bismillahpata.View.hitungHPP1.access$800(hitungHPP1.java:28)
at bismillahpata.View.hitungHPP1$9.actionPerformed(hitungHPP1.java:330)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2713)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:680)
at java.awt.EventQueue$4.run(EventQueue.java:678)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
and this my code
private void selanjutnyaActionPerformed(java.awt.event.ActionEvent evt) {
String namaProduk = this.fieldNamaProduk.getText();
double totalProduk = Double.parseDouble(fieldTotalProduk.getText());
produk p = new produk();
p.setNamaProduk(namaProduk);
p.setJumlah(totalProduk);
produkController pc = new produkController();
int res = pc.insertProduct(p);
if (res > 0){
new hitungHPP(namaProduk, totalProduk).setVisible(true);
this.dispose();
}else{
JOptionPane.showMessageDialog(null, "Perintah Gagal, silahkan isi ulang!");
}
totalHargaBahan = totalHargaBahan + total;
System.out.println(totalHargaBahan);
}
You have to use try catch with code,
private void selanjutnyaActionPerformed(java.awt.event.ActionEvent evt) {
String namaProduk = this.fieldNamaProduk.getText();
double totalProduk = Double.parseDouble(fieldTotalProduk.getText());
produk p = new produk();
p.setNamaProduk(namaProduk);
p.setJumlah(totalProduk);
produkController pc = new produkController();
int res = pc.insertProduct(p);
if (res > 0){
try{
new hitungHPP(namaProduk, totalProduk).setVisible(true);
this.dispose();
}
catch(Exception e)
{
System.out.println(e);
}
}else{
JOptionPane.showMessageDialog(null, "Perintah Gagal, silahkan isi ulang!");
}
totalHargaBahan = totalHargaBahan + total;
System.out.println(totalHargaBahan);
}
I am trying to run the following code but it gives me an error
public void addToContainer(JInternalFrame internalframe){
JInternalFrame []allFrames = Jdpmain.getAllFrames();//getting an array of all the frames in my jdesktoppane(jdpmain)
boolean flag=false;
for(int i=0;i<allFrames.length;i++){
JInternalFrame currentFrame = allFrames[i];
if(internalframe.getClass().isInstance(allFrames[i])){
Jdpmain.setSelectedFrame(currentFrame);//if the internal frame already exist it might be under another jinternalframe so, I called this method to bring it to focus
flag=true;
}
if(flag==false){
Jdpmain.add(internalframe); //if an instance of the frame I am adding does not exist in jdesktoppane it should add an instance of it to jdesktoppane
}
}
}
Exception in thread "AWT-EventQueue-0"
java.lang.IllegalArgumentException: illegal component position at
java.awt.Container.addImpl(Container.java:1093) at
javax.swing.JLayeredPane.addImpl(JLayeredPane.java:230) at
javax.swing.JDesktopPane.addImpl(JDesktopPane.java:486) at
java.awt.Container.add(Container.java:410) at
MyLibrarypackage.MainFrame.addToContainer(MainFrame.java:169) at
MyLibrarypackage.MainFrame.jButton2ActionPerformed(MainFrame.java:148)
at MyLibrarypackage.MainFrame.access$100(MainFrame.java:13) at
MyLibrarypackage.MainFrame$2.actionPerformed(MainFrame.java:62) at
javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at
javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at
javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at
javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at
javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505) at
javax.swing.JComponent.processMouseEvent(JComponent.java:3321) at
java.awt.Component.processEvent(Component.java:6270) at
java.awt.Container.processEvent(Container.java:2229) at
java.awt.Component.dispatchEventImpl(Component.java:4861) at
java.awt.Container.dispatchEventImpl(Container.java:2287) at
java.awt.Component.dispatchEvent(Component.java:4687) at
java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at
java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273) at
java.awt.Window.dispatchEventImpl(Window.java:2713) at
java.awt.Component.dispatchEvent(Component.java:4687) at
java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707) at
java.awt.EventQueue.access$000(EventQueue.java:101) at
java.awt.EventQueue$3.run(EventQueue.java:666) at
java.awt.EventQueue$3.run(EventQueue.java:664) at
java.security.AccessController.doPrivileged(Native Method) at
java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at
java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:680) at
java.awt.EventQueue$4.run(EventQueue.java:678) at
java.security.AccessController.doPrivileged(Native Method) at
java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:677) at
java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at
java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
I will be glad if anyone could help me here.
Here is the code of mouse entered event of label 1 . And the exception is occuring .I have already worked on previous version but I don't why this happening now.
private void jLabel1MouseEntered(java.awt.event.MouseEvent evt) {
ImageIcon n_cig;
n_cig = new ImageIcon(getClass().getResource("masterproject/Cfull.png"));
jLabel1.setIcon(n_cig);
}
private void jLabel1MouseExited(java.awt.event.MouseEvent evt) {
ImageIcon n_cig;
n_cig = new ImageIcon(getClass().getResource("masterproject/No Cigarretes.png"));
jLabel1.setIcon(n_cig);
}
And the exceptions are here
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:205)
at masterproject.design.jButton1ActionPerformed(design.java:384)
at masterproject.design.access$300(design.java:15)
at masterproject.design$3.actionPerformed(design.java:148)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
The reason that you get a NullPointerException is because for some reason the image file that you're trying to specify cannot be located. So the getResource() method returns a null.
Create a utility function to handle these kind of exception in future
public ImageIcon createImageIcon(String path,
String description) {
java.net.URL imgURL = getClass().getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL, description);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
Check using this function that wheather or not you are able to access the image or not
I'm Trying to run this code to get the ResultSet row by row into an Array so i can put them in a jTable. I don't get why i get this error, i'm new in this.
I would be totally thankfull if you could help me.
try {
int pos = 1;
for (int r=0;r<rsCount;r++){
String[] na = null;
res.absolute(pos);
int a=1;
while(res.next()){
na[a]=res.getString(a+1);
a+=1;
}
pos+=1;
for(int c=0;c<8;c++){
tabla.setValueAt(na[c], r, c);
}
}
} catch (SQLException ex) {
Logger.getLogger(Reporte.class.getName()).log(Level.SEVERE, null, ex);
}
}
And here is the OUTPUT:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Main.Reporte.jButton1ActionPerformed(Reporte.java:254)
at Main.Reporte.access$000(Reporte.java:17)
at Main.Reporte$1.actionPerformed(Reporte.java:78)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:688)
at java.awt.EventQueue$3.run(EventQueue.java:686)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:702)
at java.awt.EventQueue$4.run(EventQueue.java:700)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Your string array is not being properly instantiated. You need to instantiate it like:
String[] na = new String[100];
If you don't know how big the array is going to be you might want to consider using an ArrayList
ArrayList<String> na = new ArrayList<String>();
then each string can be added with:
na.add(res.getString(a+1));
Okay, so I have all of my classes inside of a few packages:
Game/src/package1/Class.class
and I want to get into a file in:
Game/res/Terrain.png
How would I do this?
public Screen(Game game){
this.game=game;
try {
URL url = new URL("/res/Terrain.png");
textures = ImageIO.read(url);
} catch (IOException e) {
e.printStackTrace();
}
}
then i get this error:
java.net.MalformedURLException: no protocol: /res/Terrain.png
at java.net.URL.<init>(URL.java:583)
at java.net.URL.<init>(URL.java:480)
at java.net.URL.<init>(URL.java:429)
at kore.survival.Screen.<init>(Screen.java:25)
at kore.survival.Game.<init>(Game.java:27)
at kore.survival.Game.main1(Game.java:99)
at kore.survival.LoginFrame.jButton1ActionPerformed(LoginFrame.java:201)
at kore.survival.LoginFrame.access$200(LoginFrame.java:16)
at kore.survival.LoginFrame$3.actionPerformed(LoginFrame.java:82)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:703)
at java.awt.EventQueue.access$000(EventQueue.java:102)
at java.awt.EventQueue$3.run(EventQueue.java:662)
at java.awt.EventQueue$3.run(EventQueue.java:660)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:676)
at java.awt.EventQueue$4.run(EventQueue.java:674)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:673)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)
Assuming that the resource is embedded with the Jar...
textures = ImageIO.read(getClass().getResource("/res/Terrain.png"));