java.io.EOFException while writing and reading froma servlet - java

I have the following code on the applet side:
URL servlet = new URL(appletCodeBase, "FormsServlet?form=requestRoom");
URLConnection con = servlet.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setUseCaches(false);
con.setRequestProperty("Content-Type", "application/octet-stream");
ObjectOutputStream out = new ObjectOutputStream(con.getOutputStream());
out.writeObject(user);//user is an object of a serializable class
out.flush();
out.close();
ObjectInputStream in = new ObjectInputStream(con.getInputStream());
status = (String)in.readObject();
in.close();
if("success".equals("status")) {
JOptionPane.showMessageDialog(rootPane, "Request submitted successfully.");
} else {
JOptionPane.showMessageDialog(rootPane, "ERROR! Request cannot be made at this
time");
}
In the servlet side I recieve the code as follows:
form = request.getParameter("form");
if("requestRoom".equals(form)) {
String fullName, eID, reason;
UserRequestingRoom user;
try {
in = new ObjectInputStream(request.getInputStream());
user = (UserRequestingRoom)in.readObject();
fullName = user.getFullName();
eID = user.getEID();
reason = user.getReason();
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/chat_applet","root","");
PreparedStatement statement = con.prepareStatement("INSERT INTO REQCONFROOM VALUES(\"" + fullName + "\",\"" + eID + "\",\"" + reason + "\")");
statement.execute();
out = new ObjectOutputStream(response.getOutputStream());
out.writeObject("success");
out.flush();
} catch (Exception e) {
e.printStackTrace();
out = new ObjectOutputStream(response.getOutputStream());
out.writeObject("fail");
out.flush();
}
}
When I click on the button that calls the code in the applet side, I get the following error:
java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source)
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
at com.org.RequestRoomForm.requestActionPerformed(RequestRoomForm.java:151)
**//Line 151 is "ObjectInputStream in..." line in the applet code**
at com.org.RequestRoomForm.access$000(RequestRoomForm.java:7)
at com.org.RequestRoomForm$1.actionPerformed(RequestRoomForm.java:62)
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.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)
Why am I getting this error? I have flushed when I output, I have closed the connections also, yet I get the error. Any reason for this?

Close the ObjectOutputStream at the end.
ObjectOutputStream out = new ObjectOutputStream(con.getOutputStream());
out.writeObject(user);//user is an object of a serializable class
out.flush();
out.close(); //Don,t close your out here
ObjectInputStream in = new ObjectInputStream(con.getInputStream());
status = (String)in.readObject();
in.close();
out.close(); //Close your out after reading the input

first request.getParameter()
then request.getInputStream()
this case must course exception on tomcat ,but not course exception on weblogic

Related

I don't konw what is wrong with my result set in java [duplicate]

This question already has answers here:
ResultSet exception - before start of result set
(6 answers)
Closed 3 years ago.
I am new in java programing and I'm trying to understand why I'm having the error below:
I have almost spent 6 hours and no luck, I have rebooted mysql server and my PC also the ip and port
are Ok. What's wrong with mt 'ResultSet'? Can some one help me with this please.
Thanks in advance for your help.
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
char[] tempPass = password.getPassword();
typedPassword = new String(tempPass);
if (!username.getText().equals("") && !typedPassword.equals("")) {
String sql = "SELECT id, username, password, admin FROM users where username=? and password=?";
try {
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, username.getText().toString());
statement.setString(2, typedPassword);
ResultSet set = statement.executeQuery();
String admin = set.getString("admin");
preferences.put("user_id", set.getString("id"));
int count = 0;
while (set.next()) {
count++;
}
if (count == 1) {
JOptionPane.showMessageDialog(button, "Login success");
if (chckbxRememberMe.isSelected()) {
DoLogin log = new DoLogin();
log.execute();
} else {
DoDelete delete = new DoDelete();
delete.execute();
}
if(admin.equals("1")){
Login.this.hide();
Admin frMain = new Admin();
frMain.setVisible(true);
frMain.show();
}
else {
Login.this.hide();
Customer frMain = new Customer();
frMain.setVisible(true);
frMain.show();
}
} else if (count > 1) {
System.out.println("Duplicate");
} else {
System.out.println("not Exists");
}
} catch (SQLException e1) {
e1.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(panel, "Fields can not be empty");
}
}
});
Here is the Error :
java.sql.SQLException: Before start of result set
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
at com.mysql.cj.jdbc.result.ResultSetImpl.checkRowPos(ResultSetImpl.java:484)
at com.mysql.cj.jdbc.result.ResultSetImpl.getString(ResultSetImpl.java:834)
at com.mysql.cj.jdbc.result.ResultSetImpl.getString(ResultSetImpl.java:852)
at msm.Login$3.actionPerformed(Login.java:175)
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$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)
Between these two lines:
ResultSet set = statement.executeQuery();
String admin = set.getString("admin");
You need to add set.next() to actually read a line from result set. Every time you run set.next() it moves to the next line. For example:
ResultSet set = statement.executeQuery();
while (set.next()) {
String admin = set.getString("admin");
...
}
The problem is at the set.getString("admin"); from the following lines.
ResultSet set = statement.executeQuery();
String admin = set.getString("admin");
After executing the query, you need to perform a set.next() before accessing the admin info, because your cursor is not moving forward otherwise (the cursor is the ResultSet)
Way of fixing it:
ResultSet set = statement.executeQuery();
if (set.next()) {
String admin = set.getString("admin");
// .. rest of your code
}

Cannot delete file because it is open due to merger class (pdf)

I am getting the following error while running this code. I found that it happens because of the merger.mergesPagesFromPdfFiles() function. How can i prevent this to happen? I tried several things but nothing worked.
java.nio.file.FileSystemException: C:\Users\Ben\Desktop\Post\Klanten\10817 - TEST TEST\Factuur TEST TEST.pdf: (Translated) The process does not have access because the file is used by another process
Merger merger = new Merger();
merger.mergesPagesFromPdfFiles(filesToSend, getPathPostTS().toString() + "\\"
+ CustomFunctions.generatePersonalFileName("Documenten", client) + ".pdf");
//Runtime.getRuntime().gc();
//merger = null;
for (String file : filesToSend) {
File fileEntry = new File(file);
try {
fileEntry.setWritable(true);
java.nio.file.Files.deleteIfExists(fileEntry.toPath());
//Another way of trying to delete it
if (fileEntry.delete()) {
System.out.println(fileEntry.getName() + " is deleted!");
} else {
System.out.println("Delete operation is failed.");
}
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println(folder.list());
if (folder.list().length > 0) {
folder.delete();
} else {
JOptionPane.showMessageDialog(null, "FOLDER IS NIET LEEG, KIJK NA");
}
And here is the merger class creating the conflict.
public Merger() {
}
public void mergesPagesFromPdfFiles(List<String> pdfFiles, String outputFileLocation) {
try {
Document document = new Document();
FileOutputStream fos = new FileOutputStream(outputFileLocation);
PdfCopy writer = new PdfCopy(document,fos );
writer.setMergeFields();
document.open();
for (String pdf : pdfFiles) {
int index = pdf.lastIndexOf("\\");
String pdfName = pdf.substring(index + 1, pdf.indexOf(".pdf"));
PdfReader reader = new PdfReader(pdf);
// if(reader.getNumberOfPages() < mergePageNr){
// System.err.println("'" + pdfName + "' has not enough pages to
// merge. Skipped to next pdf!");
// continue;
// }
writer.addDocument(reader);
}
System.out.println("Files merged!");
document.close();
writer.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Thanks for helping out!
edit: complete error plus changed code to close fos
Files merged!
java.nio.file.FileSystemException: C:\Users\Ben\Desktop\Post\Klanten\10817 - TEST TEST\Factuur TEST TEST.pdf: Het proces heeft geen toegang tot het bestand omdat het door een ander proces wordt gebruikt.
at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsFileSystemProvider.implDelete(Unknown Source)
at sun.nio.fs.AbstractFileSystemProvider.deleteIfExists(Unknown Source)
at java.nio.file.Files.deleteIfExists(Unknown Source)
at generalClasses.Generator.sendAllFilesWithPost(Generator.java:243)
at factuurGen.FactuurGenerator.(FactuurGenerator.java:63)
at factuurGen.FactuurOptionPanel.generate(FactuurOptionPanel.java:31)
at view.MainView.generateAllDocuments(MainView.java:368)
at view.MainView.lambda$1(MainView.java:309)
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$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)

Storing object in database in Java

I am trying to store my object in MySQL database using java language. I am trying to convert object into byte so I can store it into LONGBLOB. But I am facing error i.e "NotSerializable Exception".
My Class whose object I want to sore:
public class Books implements Serializable {
private int isbn;
private String bookName;
private String author;
private String edition;
private int rowNo;
private int colNo;
private String shelfNo;
private String img;
private InputStream imag;
validation v = new Validation();
Database database;
public Books() { database = new Database(); }
.
.
setters & getters...
Method to call database method to insert object in database:
String className = this.getClass().getName();
database.insertBookRecord(this.getIsbn(), this, className);
this is the object of current class which I want to store.
following is the insertBookRecord method.
public void insertBookRecord(int isbn, Books book, String name) {
String query = "INSERT INTO Test VALUES (?, ?, ?)";
byte[] data = null;
//book = new Books();
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(book);
oos.flush();
oos.close();
baos.close();
data = baos.toByteArray();
}
catch(IOException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
}
try {
//conn.setAutoCommit(false);
state = conn.prepareStatement(query);
state.setInt(1, isbn);
state.setString(2, name);
state.setObject(3, data);
state.executeUpdate();
//conn.commit();
}
catch(SQLException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
}
finally {
close(3);
}
}
When I reach at statement oos.writeObject(book); Its stops with exception and displays the the package & Classname in the JOptionPane.
My book object has all the data fields I entered in the text fields. But I am unable to write it./convert it into Serializable format.
Any suggestions please?
Stack Trace:
java.io.NotSerializableException: com.my.classes.Database
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at com.my.classes.Database.insertBookRecord(Database.java:123)
at com.my.classes.Books.insertBookRecord(Books.java:107)
at com.my.jlms.ManageBooks$2.actionPerformed(ManageBooks.java:308)
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$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)
You get the exception because you are trying to serialize an InputStream
private InputStream imag;
InputStream is not Serializable
You could omit this field when serializing by declaring it transient:
private transient InputStream imag;
Your imag is InputStream which is not implement Serializable. It should be a byte[] or String link to your imag location.
Edited: You should separate business from model object. Move insertBook method and database object from Book class to another class.
So the problem was InputStream as well as i was trying to Seralize the objects i.e V & database.
I just added transient e.g private transient Database database; and then declared database = new Database(); in the constructor. hence I was able to serialize it.
Thank you #Ralf, #Joseph, #Diyarbakir and #3kings for helping me out.

Serialize and Saving an ArrayList of double [] in Java

I want to serialize and save an arraylist that contains a double [] arraylist in java. Here is my code.
FileOutputStream fileOutputStream = null;
ObjectOutputStream objectOutputStream = null;
FileInputStream fileInputStream = null;
ObjectInputStream objectInputStream = null;
public void writeArrayListToFile(ArrayList<ArrayList<double []>> arraylist, String outputFilePath){
System.out.println("Start saving the file at : " + outputFilePath);
try {
fileOutputStream = new FileOutputStream(outputFilePath , true);
objectOutputStream = new ObjectOutputStream(objectOutputStream);
objectOutputStream.writeObject(arraylist);
System.out.println("Arraylist Saved Successfully...");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
But I get an error and following is the stack trace.
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.io.ObjectOutputStream$BlockDataOutputStream.drain(Unknown Source)
at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(Unknown Source)
at java.io.ObjectOutputStream.(Unknown Source)
at com.fyp.imur2.SaveArrayListToFile.writeArrayListToFile(SaveArrayListToFile.java:24)
at com.fyp.imur2.JPanelJFreeChart.btnSaveGestureCollectionActionPerformed(JPanelJFreeChart.java:380)
at com.fyp.imur2.JPanelJFreeChart.access$5(JPanelJFreeChart.java:375)
at com.fyp.imur2.JPanelJFreeChart$6.actionPerformed(JPanelJFreeChart.java:166)
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)
Can someone help me to solve this issue? Any help would be appreciated. Thanks in advance.
Need to pass fileOutputStream while constructing the instance of ObjectOutputStream
objectOutputStream = new ObjectOutputStream(fileOutputStream);
on the line
objectOutputStream = new ObjectOutputStream(objectOutputStream);
You try to call the constructor with the objectOutputStream object, which is null. Looks like you meant to call it with fileOutputStream, like this:
objectOutputStream = new ObjectOutputStream(fileOutputStream);

Can't fine the error with Operation not allowed after ResultSet closed

Method where im getting an error on:
public ArrayList<Log> selectAllLogs(){
if(openConn()){
ArrayList<Log> list = new ArrayList<Log>();
Boolean doesLogExists = false;
String query = "SELECT * FROM logs a LEFT JOIN log_lines s on a.log_id = s.log_id";
try{
stmt = conn.createStatement();
rs = stmt.executeQuery(query);
Log log = null;
while(rs.next()){
for (Log l : list) {
if(l.getLogId() == rs.getInt("log_id")){
log = l;
doesLogExists = true;
System.out.println("check");
}
}
if(doesLogExists){
log.getLogLines().add(rs.getString("log_lines"));
Iterator<Log> iter = list.iterator();
int i = 0;
System.out.println("true");
while(iter.hasNext()){
if(iter.next().getLogId() == ((list.get(i).getLogId()))){
list.remove(i);
list.add(i, log);
break;
}
i++;
}
doesLogExists = false;
} else {
System.out.println("false");
log = new Log();
log.setDateName(rs.getString("log_name"));
log.setLogId(rs.getInt("log_id"));
log.getLogLines().add(rs.getString("log_lines"));
list.add(log);
}
}
}
catch(SQLException sqle){
sqle.printStackTrace();
}
try {
closeConn();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
try {
closeConn();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
Stackstrace:
java.sql.SQLException: Operation not allowed after ResultSet closed
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:975)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:920)
at com.mysql.jdbc.ResultSetImpl.checkClosed(ResultSetImpl.java:804)
at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:852)
at com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5773)
at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5693)
at DataLayer.DatabaseConnector.selectAllLogs(DatabaseConnector.java:313)
at model.LogContainer.<init>(LogContainer.java:15)
at model.LogContainer.getInstance(LogContainer.java:20)
at view.LogOverview.initGui(LogOverview.java:91)
at view.LogOverview.<init>(LogOverview.java:27)
at controller.MainFrameController.actionPerformed(MainFrameController.java:65)
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)
Im getting an error on this piece of code, it is out of the else part in the while loop.
list.add(log);
I have googled the exception but i don't understand it. Can someone help me?
You can see from the stacktrace that the statement where the exception occurs in the method selectAllLogs is actually the call to com.mysql.jdbc.ResultSetImpl.getString, and not the statement list.add(log); There's an off by one error in where you thought the exception was occuring.
The reason that you're getting the SQLException is probably because you're not closing the ResultSet. This question is about the same issue.

Categories