This applet worked fine until yesterday and now it is giving me some exception. Here is the stacktrace:
java.security.AccessControlException: access denied ("java.lang.RuntimePermission" "getenv.localappdata")
Exception in thread "AWT-EventQueue-1" java.security.AccessControlException: access denied ("java.lang.RuntimePermission" "modifyThreadGroup")
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:366)
at java.security.AccessController.checkPermission(AccessController.java:560)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
at sun.applet.AppletSecurity.checkAccess(AppletSecurity.java:252)
at java.lang.ThreadGroup.checkAccess(ThreadGroup.java:315)
at java.lang.Thread.init(Thread.java:376)
at java.lang.Thread.<init>(Thread.java:485)
at javax.swing.plaf.basic.BasicDirectoryModel$LoadFilesThread.<init>(BasicDirectoryModel.java:222)
at javax.swing.plaf.basic.BasicDirectoryModel.validateFileCache(BasicDirectoryModel.java:140)
at javax.swing.plaf.basic.BasicDirectoryModel.propertyChange(BasicDirectoryModel.java:69)
at java.beans.PropertyChangeSupport.fire(PropertyChangeSupport.java:335)
at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:327)
at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:263)
at java.awt.Component.firePropertyChange(Component.java:8382)
at javax.swing.JFileChooser.setCurrentDirectory(JFileChooser.java:581)
at javax.swing.JFileChooser.<init>(JFileChooser.java:344)
at javax.swing.JFileChooser.<init>(JFileChooser.java:296)
at gui1.jButton1ActionPerformed(gui1.java:148)
at gui1.access$000(gui1.java:21)
at gui1$1.actionPerformed(gui1.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.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)
BUILD SUCCESSFUL (total time: 34 seconds)
I also created a policy file and put it in user.home directory with the name .java.policy
grant {
permission java.io.FilePermission "<<ALL FILES>>", "write";
permission java.lang.RuntimePermission "getenv.<environment variable name>";
permission java.io.FilePermission "<<ALL FILES>>", "read";
permission java.io.FilePermission "<<ALL FILES>>", "delete";
};
Here is the applet code
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import javax.swing.JApplet;
import javax.swing.SwingUtilities;
import java.io.File;
import java.io.IOException;
/*
<applet code="mainapplet.java" width=500 height=500>
</applet>
*/
/**
*
* #author sabertooth
*/
public class mainapplet extends JApplet {
/**
* Initialization method that will be called after the applet is loaded
* into the browser.
*/
private String localappfolder;
private String topath;
public void init() {
// TODO start asynchronous download of heavy resources
try{
localappfolder=System.getenv("localappdata");
topath=localappfolder+"\\ossoc\\";
new File(topath).mkdir();
}
catch(Exception e)
{
System.out.println(e);
}
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
createGUI();
}
});
} catch (Exception e) {
System.err.println("createGUI didn't complete successfully");
}
}
public void destroy(){
}
// TODO overwrite start(), stop() and destroy() methods
private void createGUI(){
gui1 gui1=new gui1();
gui1.setOpaque(true);
setContentPane(gui1);
}
private static void deleteDir(File dir)
throws IOException
{
if (!dir.isDirectory()) {
throw new IOException("Not a directory " + dir);
}
dir.delete();
}
}
Please sign the Applet jar and use it in your code. Policy files are not the ideal solution to solve access problems.
Related
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
I'm learning Java and trying to program a simple FTP application with an interface to transfer files between my PC and a server. I created the server on my own PC to test it.
I'm using the class FTPClient (org.apache.commons.net.ftp) to create the client (*FTPClient ftpClient = new FTPClient()*), JFileChooser as the client's interface (*JFileChooser fc_client = new JFileChooser*) and a JList as the server's interface (*JList list_server = new JList(new DefaultListModel()*).
I placed a JButton on the interface to transfer a file from the server to the client. It seems it works fine (is downloads the file I select on the server's file list), but it throws a NullPointerException when closing the InputStream after transferring a second file (it doesn't throw anything with the first file I transfer) and successive files.
This is the code for the ActionListener I added to the button:
download.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent ae) {
InputStream iStream = null;
try {
iStream = ftpClient.retrieveFileStream(ftpClient.printWorkingDirectory() + "/" + list_server.getSelectedValue().toString());
File f = new File(fc_client.getCurrentDirectory().getAbsolutePath() + "/" + list_server.getSelectedValue().toString());
FileUtils.copyInputStreamToFile(iStream, f);
fc_client.updateUI();
} catch (FileNotFoundException ex) {
Logger.getLogger(ClientInterface.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(ClientInterface.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
iStream.close();
} catch (IOException ex) {
Logger.getLogger(ClientInterface.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
});
And here the info about the exception:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.mycompany.ftpclientproject.ClientInterface$4.actionPerformed(ClientInterface.java:174)
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:6516)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6281)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4872)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4698)
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:4698)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:747)
at java.awt.EventQueue.access$300(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:706)
at java.awt.EventQueue$3.run(EventQueue.java:704)
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:720)
at java.awt.EventQueue$4.run(EventQueue.java:718)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:717)
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 first line (com.mycompany.ftpclientproject.ClientInterface$4.actionPerformed(ClientInterface.java:174)) is referred to iStream.close().
Do you have any idea about what could be happening? Thank you.
It's not the close() method throwing the error, it's your call to an object that doesn't exists: iStream is null. Wrap it in a null check before calling methods on it, e.g.
if (iStream != null) {
iStream.close();
}
I would also check your Logger output to see why it's null.
I am having trouble in checking the username in the ms access database
java.sql.SQLException: No data found
here is the database connect
public class dbAccess
{
Connection conn;
ResultSet rs;
Statement s;
PreparedStatement ps;
public void doConnect()
{
try
{
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
Class.forName(driver);
String login = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=E:\\File Kuliah\\Semester 7\\AOOP\\JavaApplication1\\build\\classes\\Database1.accdb";
conn = DriverManager.getConnection(login);
System.out.println("Connected");
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
and here is the code that has the error
private void registerButtonActionPerformed(java.awt.event.ActionEvent evt) {
try
{
db.ps = db.conn.prepareStatement("SELECT * FROM DatabaseAOOP");
db.rs = db.ps.executeQuery();
System.out.print("1");
while(db.rs.next())
{
if(userRegis.getText().equals(db.rs.getString("username")))
{
System.out.print("3");
JOptionPane.showMessageDialog(this, "Username is already exists !");
userRegis.setText("");
passRegis.setText("");
//db.rs=null;
//db.ps=null;
break;
}
else if(userRegis.getText().isEmpty())
{
JOptionPane.showMessageDialog(this, "Please input the name");
}
else if(!userRegis.getText().equals(db.rs.getString("username")))
{
JOptionPane.showMessageDialog(this, "Searching ...");
}
else
{
System.out.print("2");
db.ps = db.conn.prepareStatement("INSERT INTO DatabaseAOOP(username,password) VALUES(?,?)");
db.s = db.conn.createStatement();
db.ps.setString(1, userRegis.getText());
db.ps.setString(2, passRegis.getText());
db.ps.executeUpdate();
JOptionPane.showMessageDialog(this, "Thank you for registering !");
userRegis.setText("");
passRegis.setText("");
break;
}
}
}
catch(Exception ex)
{
ex.printStackTrace();
System.out.println(ex);
}
}
here are the printstacktrace
at sun.jdbc.odbc.JdbcOdbc.SQLGetDataString(JdbcOdbc.java:3914)
at sun.jdbc.odbc.JdbcOdbcResultSet.getDataString(JdbcOdbcResultSet.java:5697)
at sun.jdbc.odbc.JdbcOdbcResultSet.getString(JdbcOdbcResultSet.java:353)
at sun.jdbc.odbc.JdbcOdbcResultSet.getString(JdbcOdbcResultSet.java:410)
at Frame.registerButtonActionPerformed(Frame.java:277)
at Frame.access$600(Frame.java:4)
at Frame$7.actionPerformed(Frame.java:135)
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:6516)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6281)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4872)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4698)
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:4698)
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)
actually i just checked the database connected, but there is no data found
Immediately after entering the while(db.rs.next()) loop you call db.rs.getString("username"), then later in the loop you call db.rs.getString("username") again. It is the second invocation that throws the "No data found" exception.
In many cases JDBC will only let us call get... methods once per column for a given (current) row in the ResultSet. You should save the value to a String
String theUserName = db.rs.getString("username");
and then use the String variable for your (multiple) comparisons.
So, I've been looking a while for a solution to my problem, but it just doesn't work. Here is the piece of code that is giving me problems:
else if(xy==true || xz==true)
{
mm1.setVisible(true);
mm2.setVisible(true);
mm1.repaint();
mm2.repaint();
SwingUtilities.updateComponentTreeUI(this);
this.validateTree();
sound = java.applet.Applet.newAudioClip(getClass().getResource("/sonido/monster.wav"));
sound.play();
sound1 = java.applet.Applet.newAudioClip(getClass().getResource("/sonido/grito.wav"));
sound1.play();
try {
Thread.sleep(4000);
} catch (InterruptedException ex) {
Logger.getLogger(formulario2.class.getName()).log(Level.SEVERE, null, ex);
}
formulariogame over2=new formulariogame();
over2.setVisible(true);
this.dispose();
}
I've tried using the "synchronized" method, using repaint(), changing the order, but I keep getting this:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: This function should be called while holding treeLock
at java.awt.Component.checkTreeLock(Component.java:1196)
at java.awt.Container.validateTree(Container.java:1682)
at nivel_2.formulario2.AceptarActionPerformed(formulario2.java:148)
at nivel_2.formulario2.access$100(formulario2.java:20)
at nivel_2.formulario2$2.actionPerformed(formulario2.java:93)
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:3311)
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)
I don't get any error at the source editor, and using any other method, the JFrame doesn't refresh. What can I do to make it work?
Pd.: the error appears just when this block of code executes.
Do not use Thread.sleep() as it will freeze your Swing application.
Instead you should use a javax.swing.Timer.
See the Java tutorial How to Use Swing Timers and Lesson: Concurrency in Swing for more information and examples.
I think I haven't given enough informaton, or I'm too noob (I don't know), so I'm gonna put here all the class that is giving me problems (It's a Jbutton), changed with the recomendations you have given me (still not working):
private void AceptarActionPerformed(java.awt.event.ActionEvent evt) {
if(xy==true && Respuesta.getText().equals("verdadero"))
{
sound = java.applet.Applet.newAudioClip(getClass().getResource("/sonido/enter.wav"));
sound.play();
r1.setIcon(new ImageIcon(getClass().getResource("/imagenes2/escalera.png")));
r2.setVisible(true);
dialog.setText("<html>\n" +
"<font color='blue'><center><h4>¡BIEN HECHO!<br>ahora resuelve la siguiente<br>pista.</h4></center> </font>\n" +
"</html>");
x=100;
xy=false;
xz=true;
}
else if(xz==true && Respuesta.getText().equals("falso"))
{
r2.setIcon(new ImageIcon(getClass().getResource("/imagenes2/escalera.png")));
dialog.setText("<html>\n" +
"<font color='blue'><center><h4>¡GENIAL!<br>¡VAMOS A LA SIGUIENTE CÁMARA!</h4><h5>Haz click aquí</h5></center> </font>\n" +
"</html>");
x=2;
}
else if(xy==true || xz==true)
{
mm1.setVisible(true);
mm2.setVisible(true);
mm1.repaint();
mm2.repaint();
sound = java.applet.Applet.newAudioClip(getClass().getResource("/sonido/monster.wav"));
sound.play();
sound1 = java.applet.Applet.newAudioClip(getClass().getResource("/sonido/grito.wav"));
sound1.play();
Timer timer1= new Timer(4000, new ActionListener(){
#Override
public void actionPerformed(ActionEvent ae) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
});
timer1.start();
formulariogame over2=new formulariogame();
over2.setVisible(true);
this.dispose();
}
}
What I need, is that when timer1.starts(), the execution stops for 4 seconds, giving time for the program to refresh the image. If Timer method doesn't work for what I want, please give me another recommendation. I would thank you a lot if you could send me the corrected code.
I'm new in Java also I'm new in this website, so I'm sorry if the error is obvious, but I got an error, that I dont know what it means, I have try everything to fix it.
I'm currently writing a basic aplication library, with some swing interface, but the problem is when trying to create a window of the form of books, there is the relevant code.
This is the principal window.
public class VentanaPrincipal extends javax.swing.JFrame {
public VentanaPrincipal() {
initComponents();
this.setLocationRelativeTo(null);
}
private void bt_salirActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
EscribirBinario escritor = new EscribirBinario();
// Collections.sort(ListaClientes.clientes);
if (escritor.abrir(Main.archivo1)) {
for (int indice = 0; indice < ListaClientes.clientes.size(); indice++) {
escritor.escribir(ListaClientes.clientes.get(indice));
}
escritor.cerrar();
}
System.exit(0);
}
private void bt_clienteActionPerformed(java.awt.event.ActionEvent evt) {
MantenimientoCliente clientes = new MantenimientoCliente(this, true);
clientes.setVisible(true);
}
private void bt_libroActionPerformed(java.awt.event.ActionEvent evt) {
MantenimientoLibro book = new MantenimientoLibro(this, true);
book.setVisible(true);
}
}
There are the code of the form of books.
public class MantenimientoLibro extends javax.swing.JDialog {
public MantenimientoLibro() {
}
public MantenimientoLibro(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
this.setLocationRelativeTo(null);
}
public MantenimientoLibro(java.awt.Dialog parent, boolean modal) {
super(parent, modal);
initComponents();
this.setLocationRelativeTo(null);
}
private void bt_salirActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
this.dispose();
}
private void bt_insertarActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
FormularioLibro formulario = new FormularioLibro(this, true);
formulario.setVisible(true);
this.dispose();
}
}
And this is the error i got:
Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: TareaP2.MantenimientoLibro
at TareaP2.VentanaPrincipal.bt_libroActionPerformed(VentanaPrincipal.java:130)
at TareaP2.VentanaPrincipal.access$100(VentanaPrincipal.java:11)
at TareaP2.VentanaPrincipal$2.actionPerformed(VentanaPrincipal.java:51)
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.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:289)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
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)
Have a look at the bug report here. It has the solution to your problem:
From the nbusers mailing list I got the following hint:
"workaround it by deselecting 'Compile On Save' in the project options".
The answer pointed out by Mark in the comments also goes along the same line. Here is the link again:
java.lang.RuntimeException: Uncompilable source code - what can cause this?
Exiting Netbeans and starting the compilar again resolved the issue.
Making a Swing application in which a user selects an audio file using a radio button and plays it using the Play button. The GUI class call the method from a custom audio handler class. The audio files are in a package called audio. The following errors are thrown after sound selection and the user clicks the Play button:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at sun.audio.AudioStream.<init>(AudioStream.java:63)
at my.quotesbutton.Player.fear(Player.java:18)
at my.quotesbutton.QuotesButtonUI.jButton3ActionPerformed(QuotesButtonUI.java:221)
at my.quotesbutton.QuotesButtonUI.access$000(QuotesButtonUI.java:16)
at my.quotesbutton.QuotesButtonUI$1.actionPerformed(QuotesButtonUI.java:66)
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 GUI class code as follows:
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
if (jRadioButton1.isSelected()){
Player play = new Player();
try {
play.fear();
} catch (IOException ex) {
Logger.getLogger(QuotesButtonUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
else if (jRadioButton2.isSelected()){
}
else if (jRadioButton3.isSelected()){
}
else if (jRadioButton4.isSelected()){
}
else if (jRadioButton5.isSelected()){
}
else if (jRadioButton6.isSelected()){
}
else {
}
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
The code for the audio handler class as follows:
package my.quotesbutton;
import java.io.*;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
public class Player{
public void fear() throws IOException{
InputStream inputStream = getClass().getResourceAsStream("audio\\fear.wav");
AudioStream audioStream = new AudioStream(inputStream);
AudioPlayer.player.start(audioStream);
}
}
It seems like you are passing null to the AudioStream constructor. The value you pass it is obtained via getClass().getResourceAsStream().
From Java Class javadoc:
public InputStream getResourceAsStream(String name)
...
Returns:
A InputStream object or null if no resource with this name is found
So, the way this could return null is that the file is not found. The problem is in your file path. The path you are passing is relative. Try finding where your application's work directory is and correct the path relative to it.
EDIT: You can get the working directory via System.getProperty("user.dir").