Java calls php to retrieve SQL data - java

I'm hoping someone can help figure this out. I've got a problem trying to run some backend php code from java that extracts sql data and sends it back via gson. I've used posting from java to php and inserting it into mysql database as a model, but, I can't seem to get beyond the HTTP 400 response code that is returned. I've edited the following java console to remove identifying names, but, the context should remain the same.
security: JSS is not configured
network: Connecting https://secure.justhost.com/<server>/<folder>/<php program name>.php with proxy=DIRECT
network: Cache entry not found [url: http://secure.justhost.com:443/crossdomain.xml, version: null]
network: Connecting http://secure.justhost.com:443/crossdomain.xml with proxy=DIRECT
network: Connecting http://secure.justhost.com:443/ with proxy=DIRECT
java.io.IOException: Server returned HTTP response code: 400 for URL: http://secure.justhost.com:443/crossdomain.xml
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at com.sun.deploy.net.CrossDomainXML.check(Unknown Source)
at com.sun.deploy.net.CrossDomainXML.check(Unknown Source)
at sun.plugin2.applet.SecurityManagerHelper.checkConnectHelper(Unknown Source)
at sun.plugin2.applet.AWTAppletSecurityManager.checkConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.<init>(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.New(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.HttpURLConnection.getResponseCode(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(Unknown Source)
at newposting.GetCategories(newposting.java:864)
at newposting.<init>(newposting.java:170)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at com.sun.deploy.uitoolkit.impl.awt.AWTAppletAdapter$1.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.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)
basic: exception: java.lang.reflect.InvocationTargetException.
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.sun.deploy.uitoolkit.impl.awt.AWTAppletAdapter.runOnEDTAndWait(Unknown Source)
at com.sun.deploy.uitoolkit.impl.awt.AWTAppletAdapter.instantiateApplet(Unknown Source)
at sun.plugin2.applet.Plugin2Manager.initAppletAdapter(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
at com.sun.deploy.uitoolkit.impl.awt.OldPluginAWTUtil.invokeAndWait(Unknown Source)
... 5 more
Caused by: java.lang.RuntimeException: java.security.AccessControlException: access denied ("java.net.SocketPermission" "secure.justhost.com:443" "connect,resolve")
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getHeaderField(Unknown Source)
at java.net.HttpURLConnection.getResponseCode(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(Unknown Source)
at newposting.GetCategories(newposting.java:864)
at newposting.<init>(newposting.java:170)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at com.sun.deploy.uitoolkit.impl.awt.AWTAppletAdapter$1.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.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)
Caused by: java.security.AccessControlException: access denied ("java.net.SocketPermission" "secure.justhost.com:443" "connect,resolve")
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkConnect(Unknown Source)
at sun.plugin2.applet.SecurityManagerHelper.checkConnectHelper(Unknown Source)
at sun.plugin2.applet.AWTAppletSecurityManager.checkConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.<init>(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.New(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
... 29 more
security: Reset deny session certificate store
The url for the php code has no spaces. It does use underlines ("_") to join words. The request that is show in the console does not pass any parameters to the url because the php code has all it needs to retrieve the data. The java code is simply calling the backend php code. Here's a look at what I've got for the java code.
private ArrayList<String> GetCategories ()
{
ArrayList<String> cats = new ArrayList<String> ();
// preparation
String charset = "UTF-8";
String szURL = new String ();
if (!debugConfiguration)
szURL = "https://secure.justhost.com/<server>/<folder>/<php program name>.php";
else
szURL = "http://localhost/<php program name>.php";
// HTTP Post request
URL object = null;
try
{
object = new URL(szURL);
}
catch (MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
HttpsURLConnection con = null;
try
{
con = (HttpsURLConnection) object.openConnection();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
//add request header
try
{
con.setRequestMethod("POST");
}
catch (ProtocolException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
con.setRequestProperty(charset, "utf-8");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
// Send post request
//con.setDoOutput(true);
/*
DataOutputStream wr = null;
try
{
wr = new DataOutputStream (con.getOutputStream());
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
try
{
wr.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
*/
try
{
int responseCode = con.getResponseCode();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader in = null;
try
{
in = new BufferedReader(new InputStreamReader(con.getInputStream()));
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
String inputLine;
StringBuffer response = new StringBuffer();
try
{
while ((inputLine = in.readLine()) != null)
{
response.append(inputLine);
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
try
{
in.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
Gson gson = new Gson ();
jsonArticleCategories[] enums = gson.fromJson(response.toString(), jsonArticleCategories[].class);
for (int i=0; i<enums.length; ++i)
{
jsonArticleCategories jac = enums[i];
cats.add(jac.get_category());
}
return cats;
}
Again, I've edited the code sample to remove some names, but the context is valid. There are not spaces in any of the names for the server, folder, or program name. Lastly, let me add that the java code is digitally signed with a code signing certificate and run through jnlp. The php code runs correctly if executed manually from a browser.
Thanks for any help.

Related

"java.sql.SQLException: No database selected" with MySQL JDBC [duplicate]

This question already exists:
Error:java.sql.SQLSyntaxErrorException: Table 'loginpass.intousers' doesn't exist [duplicate]
Closed 3 years ago.
Code:
Connection dbConnection;
public Connection getDbConnection() throws ClassNotFoundException, SQLException{
String connectionString = "jdbc:mysql://127.2.0.1:3306/?user=root?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&useSSL=false";
;
String url="?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
Class.forName("com.mysql.jdbc.Driver");
dbConnection = DriverManager.getConnection(connectionString, dbUser, dbPass);
return dbConnection;
}
public void SingUpUser(String id,String login, String password) {
String insert = "INSERT INTO"+Constant.USER_TABLE+"("+Constant.USERS_ID+","+Constant.USERS_LOGIN+","+Constant.USER_PASSWORD+")"+
"VALUES(?,?,?)";
try {
PreparedStatement prSt = getDbConnection().prepareStatement(insert);
prSt.setString(1, id);
prSt.setString(2, login);
prSt.setString(3, password);
prSt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Error:
java.sql.SQLException: No database selected
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.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:955)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1094)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1042)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeLargeUpdate(ClientPreparedStatement.java:1345)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdate(ClientPreparedStatement.java:1027)
at minelaunch.Databases.SingUpUser(Databases.java:36)
at minelaunch.minelauncherv1$5.actionPerformed(minelauncherv1.java:182)
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)
I expect to write to the database, but I get this error.
From the stack trace:
java.sql.SQLException: No database selected
You have removed the database name from your connection string, so now MySQL does not know which database you want to work with. Edit your connection string again and specify the actual name of the database you want to use.

errors when replace phantomjs to headless chromedriver

i got many errors about js... so i changed to chromedriver headless, and it work better for screenshot a specific element, but i got an errors in a other screenshot(s) code
Starting ChromeDriver 2.45.615291 (ec3682e3c9061c10f26ea9e5cdcf3c53f3f74387)
on port 44903
Only local connections are allowed.
janv. 31, 2019 7:02:22 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFOS: Detected dialect: OSS
Exception in thread "AWT-EventQueue-0" java.awt.image.RasterFormatException: (y + height) is outside of Raster
at sun.awt.image.ByteInterleavedRaster.createWritableChild(Unknown Source)
at java.awt.image.BufferedImage.getSubimage(Unknown Source)
at CarteEtdInfo.photoProfile(CarteEtdInfo.java:57)
at Accueil.<init>(Accueil.java:99)
at Login$2.actionPerformed(Login.java:287)
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.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicRootPaneUI$Actions.actionPerformed(Unknown Source)
at javax.swing.SwingUtilities.notifyAction(Unknown Source)
at javax.swing.JComponent.processKeyBinding(Unknown Source)
at javax.swing.KeyboardManager.fireBinding(Unknown Source)
at javax.swing.KeyboardManager.fireKeyboardAction(Unknown Source)
at javax.swing.JComponent.processKeyBindingsForAllComponents(Unknown Source)
at javax.swing.JComponent.processKeyBindings(Unknown Source)
at javax.swing.JComponent.processKeyEvent(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.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(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)
this is the the code that have errors from class CarteEtdInfo
public void photoProfile() throws IOException {
String cookie = String.join("\n",Files.readAllLines(Paths.get("temp\\cookie.txt")));
Login webpage = new Login();
WebDriver pagee = webpage.driver;
pagee.get("https://www4.inscription.tn/ORegMx/servlet/AuthentificationEtud?Idsession="+cookie+"&action1=toCarteEtd");
// Get entire page screenshot
WebElement taswira = driver.findElement(By.xpath("/html[1]/body[1]/table[1]/tbody[1]/tr[1]/td[1]/table[1]/tbody[1]/tr[1]/td[1]/table[2]/tbody[1]/tr[4]/td[1]/div[2]/div[1]/table[2]/tbody[1]/tr[2]/td[1]/img[1]"));
File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
BufferedImage fullImg = null;
try {
fullImg = ImageIO.read(screenshot);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Get the location of element on the page
org.openqa.selenium.Point point = taswira.getLocation();
// Get width and height of the element
int eleWidth = taswira.getSize().getWidth();
int eleHeight = taswira.getSize().getHeight();
// Crop the entire page screenshot to get only element screenshot
BufferedImage eleScreenshot = fullImg.getSubimage(point.getX(), point.getY(), eleWidth, eleHeight); // line 57
try {
ImageIO.write(eleScreenshot, "png", screenshot);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Copy the element screenshot to disk
File screenshotLocation = new File("temp\\avatar.png");
try {
FileUtils.copyFile(screenshot, screenshotLocation);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this is the line 57
BufferedImage eleScreenshot = fullImg.getSubimage(point.getX(), point.getY(), eleWidth, eleHeight);
the preivious code of screenshot an element in Login class works fine, so why this one have a problem ?
also the chromedriver not headless works fine but the headless not
Headless chrome default window size might be smaller than expected and this might cause your element to be outside of the window, hence not visible. You can either set the widndow size yourself or maximize window by using:
chromeOptions.addArguments(""--start-maximized")
i fixed the problem by adding
chromeOptions.addArguments("window-size=1980,960");

Java SocketException Error

I'm trying to understand how an API works to develop using it.
I need to connect in that API and authenticate.
The authentication method is POST.
Here is my code:
try {
URL url = new URL("http://api.olhovivo.sptrans.com.br/v0/Login/Autenticar");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.addRequestProperty("token", "77b495072cf8b35a0b187218721871e4790dd6b92asxald6a29");
int code = connection.getResponseCode();
System.out.println("Response Code: " + code);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Here is the error I get:
java.net.SocketException: Permission denied: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.<init>(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.HttpURLConnection.getResponseCode(Unknown Source)
at TestandoAPIOlhoVivo.main(TestandoAPIOlhoVivo.java:19)
What am I doing wrong?

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