java.lang.NullPointerException: Cannot invoke method addURL() on null object [duplicate] - java

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
Below are the first few lines of my groovy file. When i try to run it from the server, i get the null Pointer Exception: Cannot invoke method addURL() on null object
I am not sure how the code needs to be changed here to fix the above error:
package groovy;
def localFile = new File("/tmp/sqljdbc4.jar");
this.class.classLoader.rootLoader.addURL(localFile.toURI().toURL());
import java.sql.Connection;
import java.sql.DriverManager;
import com.microsoft.sqlserver.jdbc.*;
import groovy.sql.Sql;
Connection ret = null;
try {
SQLServerDataSource ds = new SQLServerDataSource()
......

Try using the class name instead of this for example if the class is named Groove use the line Groove.class.classLoader.rootLoader.addURL(localFile.toURI().toURL());

Related

NoClassDefFoundError: com/mongodb/MongoClientURI [duplicate]

This question already has answers here:
Why am I getting a NoClassDefFoundError in Java?
(31 answers)
Closed 2 years ago.
package com.sm.mongo;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.types.ObjectId;
public class JavaMongoConnection {
public static void main(String[] args) {
//System.setProperty("jdk.tls.trustNameService", "true");
MongoClientURI uri = new MongoClientURI(
"mongodb+srv://admin:admin123#cluster0-bkruu.mongodb.net/test?retryWrites=true&w=majority");
MongoClient mongoClient = new MongoClient(uri);
MongoDatabase database = mongoClient.getDatabase("test");
}
}
This is my code, and whenever I run this code, I get the error:
Exception in thread "main" java.lang.NoClassDefFoundError: com/mongodb/MongoClientURI
I looked at java.lang.NoClassDefFoundError when using MongoDB driver and I tried their solution where I went to Run Configurations --> Dependencies and looked under Classpath Entries to make sure that I have bson-xxx.jar, mongodb-driver-xxx.jar, and mongodb-driver-core-xxx.jar listed. I have these listed and despite this, I keep getting the same error as in that stackoverflow post.
Any help would be appreciated.
com/mongodb/MongoClientURI class is present in mongo-java-driver jar. https://mongodb.github.io/mongo-java-driver/
Including the above jar in classpath should fix the issue

Java Equivalent of c# this.GetType().Namespace [duplicate]

This question already has answers here:
How to Get Package Name of Source Class File in Android ?
(5 answers)
Is there any equivalent syntax for C# Type.GetType() in java
(2 answers)
How to get current class name including package name in Java?
(4 answers)
Closed 4 years ago.
Which is the java equivalent of c#
this.GetType().Namespace
to get the package name in which the class is contained?
This is the package of a class:
Package myPackage = getClass().getPackage();
// or from a static context:
Package myPackage = MyClass.class.getPackage();
as a String:
String packageName = getClass().getPackage().getName();
// or from a static context:
String packageName = MyClass.class.getPackage().getName();

While executing the java code i am getting below error.please help me fixing this [duplicate]

This question already has answers here:
java.lang.Error: Unresolved compilation problems : WebDriver/ChromeDriver cannot be resolved to a type error while executing selenium tests
(5 answers)
Closed 4 years ago.
Code:
package demo1;
public class helloworld {
public static void main(String[] args) {
// TODO Auto-generated method stub
// Create a new instance of the Firefox driver
WebDriver driver = new FirefoxDriver();
//Launch the Online Store Website
driver.get("http://www.store.demoqa.com");
// Print a Log In message to the screen
System.out.println("Successfully opened the website www.Store.Demoqa.com");
//Wait for 5 Sec
Thread.sleep(5000);
Error:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
WebDriver cannot be resolved to a type
FirefoxDriver cannot be resolved to a type
at demo1.helloworld.main(helloworld.java:8)
Import these two lines :
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
Assuming all pre-requisites have already been taken care.
You can use use selenium-java.x.jar in your classpath.
Compile and run...

ERROR:: No suitable driver found for jdbc:.. localhost 1572//project [duplicate]

This question already has answers here:
How to fix: "No suitable driver found for jdbc:mysql://localhost/dbname" error when using pools? [duplicate]
(21 answers)
SQLException: No suitable driver found for jdbc:derby://localhost:1527
(19 answers)
Closed 6 years ago.
I have been looking forever how to connect to a database using java.
I Just used netbeans to create the data base, the url is
jdbc:derby://localhost:1527/project
However im using the terminal to compile and execute the java code instead of netbeans but I still have the error message that the no suitable driver found etc.. Here is the code. If anyone can help.
Thank you very much, it is really appreciated. I know that my question have been posted by I have tried everything and it dosnt work.
import java.sql.*;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.Statement;
import com.mysql.jdbc.Driver;
public class Test {
public static void main (String[] args){
String host="jdbc:derby://localhost:1527/project";
String name="groupe3";
String password= "password";
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn= DriverManager.getConnection(host,name,password);
Statement insert=conn.createStatement();
insert.executeUpdate("INSERT INTO Login VALUES('apple',2)");
insert.close();
}
catch(SQLException err){
System.out.println(err.getMessage());}
catch (ClassNotFoundException e) {
e.printStackTrace(); }
}
}
It looks like you've created a derby database, and are trying to use a mysql database driver.
https://db.apache.org/derby/docs/10.4/devguide/cdevdvlp40653.html

Eclipse is unable to locate System.in in java code [duplicate]

This question already has answers here:
What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?
(18 answers)
Closed 6 years ago.
I have the code:
import java.io.*;
...
public void test() {
...
InputStreamReader inputStream = new InputStreamReader(System.in);
...
}
but eclipse is not allowing the "in" part of the System.in.
When placing the '.' after System, I get offered many alternatives, but not 'in'.
the error message is given in eclipse is: "in cannot be resolved or is not a field".
I've been 'round in circles with it, but cannot see why - anyone have any ideas?
Running Eclipse on Debian Jessie.
Hope this helps, modify it as:
InputStreamReader inputStream = new InputStreamReader(java.lang.System.in);

Categories