java.lang.ClassNotFoundException, although the class exists - java

I got java.lang.ClassNotFoundException:
Exception in thread "main" java.lang.NoClassDefFoundError: Consts/SortOfPages
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.getMainMethod(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: Consts.SortOfPages
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 6 more
and this is the class of SortOfPages:
package Consts;
public enum SortOfPages {
POPUP("New - "),
SILENT("Silent - ");
public String description;
private SortOfPages(String description) {
this.description = description;
}
public String getDescription() {
return description;
}
}
in the import of the main class:
import Consts.SortOfPages;
But when I search the Const/SortOfPages, I find it, and it exists.
I try to clean, and build again - and the same.
How Can I Solve it?

Related

java.lang.ClassNotFoundException when trying to load derived class

I have two different Modules: MultiRealm and SurvivalRealm. MultiRealm provides an abstract class (LoadedRealm) which the class SurvivalRealm (in the Module SurvivalRealm) extends. Now, MultiRealm tries to load the SurvivalRealm class from the jar. But when trying to load, i get the Following error: java.lang.NoClassDefFoundError: net/lightbluefoxlabs/dev/multirealm/core/multirealmcore/Realms/LoadedRealm where net/lightbluefoxlabs/dev/multirealm/core/multirealmcore/Realms/LoadedRealm is the correct location of the LoadedRealm class. Here is the Stacktrace:
[23:03:52] [Thread-10/WARN]: at java.lang.ClassLoader.defineClass1(Native Method)
[23:03:52] [Thread-10/WARN]: at java.lang.ClassLoader.defineClass(Unknown Source)
[23:03:52] [Thread-10/WARN]: at java.security.SecureClassLoader.defineClass(Unknown Source)
[23:03:52] [Thread-10/WARN]: at java.net.URLClassLoader.defineClass(Unknown Source)
[23:03:52] [Thread-10/WARN]: at java.net.URLClassLoader.access$100(Unknown Source)
[23:03:52] [Thread-10/WARN]: at java.net.URLClassLoader$1.run(Unknown Source)
[23:03:52] [Thread-10/WARN]: at java.net.URLClassLoader$1.run(Unknown Source)
[23:03:52] [Thread-10/WARN]: at java.security.AccessController.doPrivileged(Native Method)
[23:03:52] [Thread-10/WARN]: at java.net.URLClassLoader.findClass(Unknown Source)
[23:03:52] [Thread-10/WARN]: at java.lang.ClassLoader.loadClass(Unknown Source)
[23:03:52] [Thread-10/WARN]: at java.lang.ClassLoader.loadClass(Unknown Source)
[23:03:52] [Thread-10/WARN]: at net.lightbluefoxlabs.dev.multirealm.core.multirealmcore.Realms.LoadedRealm.RealmFactory(LoadedRealm.java:54)
[23:03:52] [Thread-10/WARN]: at net.lightbluefoxlabs.dev.multirealm.core.multirealmcore.Realms.LoadedRealm.RealmFactory(LoadedRealm.java:20)
[23:03:52] [Thread-10/WARN]: at net.lightbluefoxlabs.dev.multirealm.core.multirealmcore.Realms.RealmManager.Initialize(RealmManager.java:22)
[23:03:52] [Thread-10/WARN]: at net.lightbluefoxlabs.dev.multirealm.core.multirealmcore.Realms.RealmManager.lambda$InitializeAsync$0(RealmManager.java:39)
[23:03:52] [Thread-10/WARN]: at java.lang.Thread.run(Unknown Source)
[23:03:52] [Thread-10/WARN]: Caused by: java.lang.ClassNotFoundException: net.lightbluefoxlabs.dev.multirealm.core.multirealmcore.Realms.LoadedRealm
[23:03:52] [Thread-10/WARN]: at java.net.URLClassLoader.findClass(Unknown Source)
[23:03:52] [Thread-10/WARN]: at java.lang.ClassLoader.loadClass(Unknown Source)
[23:03:52] [Thread-10/WARN]: at java.lang.ClassLoader.loadClass(Unknown Source)
[23:03:52] [Thread-10/WARN]: ... 16 more
Here is the SurvivalRealm class:
import net.lightbluefoxlabs.dev.multirealm.core.multirealmcore.Realms.LoadedRealm;
import net.lightbluefoxlabs.dev.multirealm.core.multirealmcore.Realms.Worlds.RealmWorld;
public class SurvivalRealm extends LoadedRealm {
public SurvivalRealm(String RealmPath) {
super(RealmPath);
Load();
}
#Override
public void Load() {
//LoadStuff
}
#Override
public void Unload() {
}
}
... and this static function in LoadedRealm tries to import this class:
public static LoadedRealm RealmFactory(String jarPath, String RealmPath) throws RealmLoadException{
try {
File file = new File(jarPath);
URLClassLoader c = new URLClassLoader(new URL[]{file.getAbsoluteFile().toURI().toURL()});
Class<?> realmClass = c.loadClass(RealmClass);
return (LoadedRealm)(realmClass.getDeclaredConstructor(String.class).newInstance(RealmPath));
}catch (Exception e){
throw new RealmLoadException(e);
}
}
Here is a picture of the Structure:
The MultiRealm Module does not import/"Know about" (excuse my terminology) the SurvivalRealm module, only the other way around.
Am I just trying to achieve something impossible? If you need the .iml file, just ask. Any help would be really apreciated, I have been trying to fix this issue for basically the whole day now.
Thanks for reading through this beginner issue.
You should try using your pom files from maven for achieve your goal. A multi-module project is defined by a parent POM referencing one or more submodules.

Why cannot I serialize JTable if the data of table is modified by user?

I have a class CustomTable with 2 blank rows and columns.
class CustomTable extends JTable{
CustomTable(){
super(2,2);
}
}
I have a save button that serializes CustomTable...
saveButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
try{
FileOutputStream file = new FileOutputStream("file.ser");
ObjectOutputStream out = new ObjectOutputStream(file);
tab = new CustomTable();
out.writeObject(tab);
out.close();
file.close();
}
catch(IOException ex)
{
ex.printStackTrace();
}
}
});
When I click save button without editing the cells, serialization, and deserialization works properly. (Also with data entered during initializing CustomTable).
But when I edit data of the cells during runtime and press save, it shows NotSerializableException.
I am not getting where I am doing it wrong.
EDIT 1:
I have tried extending DefaultTableModel instead of JTable (obviously changing the above code a bit), but everything works well only till i dont edit cells during runtime.
How do i serialize it(JTable / DefaultTableModel) after the user has entered data into cells?
Here is the exception :
java.io.NotSerializableException: java.lang.reflect.Constructor
at java.base/java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.base/java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
at java.base/java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.base/java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.base/java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.base/java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
at java.base/java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.base/java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.base/java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.base/java.io.ObjectOutputStream.writeObject(Unknown Source)
at java.desktop/javax.swing.event.EventListenerList.writeObject(Unknown Source)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
at java.base/java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
at java.base/java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.base/java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.base/java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.base/java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
at java.base/java.io.ObjectOutputStream.defaultWriteObject(Unknown Source)
at java.desktop/javax.swing.JComponent.writeObject(Unknown Source)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
at java.base/java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
at java.base/java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.base/java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.base/java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.base/java.io.ObjectOutputStream.writeArray(Unknown Source)
at java.base/java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.base/java.io.ObjectOutputStream.access$300(Unknown Source)
at java.base/java.io.ObjectOutputStream$PutFieldImpl.writeFields(Unknown Source)
at java.base/java.io.ObjectOutputStream.writeFields(Unknown Source)
at java.desktop/java.awt.Container.writeObject(Unknown Source)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
at java.base/java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
at java.base/java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.base/java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.base/java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.base/java.io.ObjectOutputStream.writeObject(Unknown Source)
at java.desktop/javax.swing.event.EventListenerList.writeObject(Unknown Source)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
at java.base/java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
at java.base/java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.base/java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.base/java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.base/java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
at java.base/java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.base/java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.base/java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.base/java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
at java.base/java.io.ObjectOutputStream.defaultWriteObject(Unknown Source)
at java.desktop/javax.swing.JTable.writeObject(Unknown Source)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
at java.base/java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
at java.base/java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.base/java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.base/java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.base/java.io.ObjectOutputStream.writeObject(Unknown Source)
at com.kdhitesh.layout.LayoutManager$1.actionPerformed(LayoutManager.java:57)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at java.desktop/javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.desktop/java.awt.Component.processMouseEvent(Unknown Source)
at java.desktop/javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.desktop/java.awt.Component.processEvent(Unknown Source)
at java.desktop/java.awt.Container.processEvent(Unknown Source)
at java.desktop/java.awt.Component.dispatchEventImpl(Unknown Source)
at java.desktop/java.awt.Container.dispatchEventImpl(Unknown Source)
at java.desktop/java.awt.Component.dispatchEvent(Unknown Source)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.desktop/java.awt.Container.dispatchEventImpl(Unknown Source)
at java.desktop/java.awt.Window.dispatchEventImpl(Unknown Source)
at java.desktop/java.awt.Component.dispatchEvent(Unknown Source)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.desktop/java.awt.EventQueue.access$600(Unknown Source)
at java.desktop/java.awt.EventQueue$4.run(Unknown Source)
at java.desktop/java.awt.EventQueue$4.run(Unknown Source)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.desktop/java.awt.EventQueue$5.run(Unknown Source)
at java.desktop/java.awt.EventQueue$5.run(Unknown Source)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.desktop/java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.desktop/java.awt.EventDispatchThread.run(Unknown Source)

JavaFX Reading from file throws "InvocationTargetException"?

I've been trying to understand what the issue is exactly, but whatever I do doesn't seem to work.
I have a text file that lists names along with numbers, seperated by a colon.
An example of this is:
Betty Ross:52
Angie Scotts:29
Michael Rosen:72
The list is very long and comprises over 10,000 lines.
public class PeopleIds {
public static int UNDEFINED_ID = -1;
private static HashMap<String, Integer> people;
public static void initialize() {
people = new HashMap<String, Integer>();
System.out.println(new File("res/ids/people_ids.txt").exists());
try {
Files.lines(Paths.get("res/ids/people_ids.txt")).forEach(s -> {
people.put(s.replaceAll(":.*", "").trim(), Integer.parseInt(s.replaceAll(".*:", "")));
});
} catch (IOException e) {
System.out.println("Unable to read specified file.");
e.printStackTrace();
}
}
public static int getId(final String name) {
final Integer id = people.get(name);
return id != null ? id : UNDEFINED_ID;
}
}
I call the initialize method from a GUIController class:
public class GUIController implements Initializable {
#FXML
private TableView<PersonData> personTable;
#FXML
private TableColumn<PersonData, String> name;
#FXML
private TableColumn<PersonData, Integer> limit;
#FXML
private TextField searchInput;
#FXML
private ImageView personIcon;
private Image undefinedIcon;
private PersonIcon icon;
private ObservableList<PersonData> data;
#Override
public void initialize(URL location, ResourceBundle resources) {
PeopleIds.initialize();
undefinedIcon = new Image(getClass().getResourceAsStream("/ids/no.png"));
name.setCellValueFactory(new PropertyValueFactory<PersonData, String>("name"));
limit.setCellValueFactory(new PropertyValueFactory<PersonData, Integer>("limit"));
data = PriceData.getData();
personTable.setPeople(data);
searchInput.textProperty().addListener((ov, oldValue, newValue) -> {
final String input = searchInput.getText();
if (input.length() == 0) return;
searchInput.setText(input.substring(0, 1).toUpperCase() + input.substring(1).toLowerCase());
filterSearch();
});
}
}
When I call it from this class with PeopleIds.initialize(), an exception is thrown, telling me that there was an exception in the application start method.
Here is what was logged in its entirety:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$156(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: javafx.fxml.LoadException:
/C:/Confidential/bin/base/PersonGUI.fxml
at javafx.fxml.FXMLLoader.constructLoadException(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at base.PersonGUI.start(PersonGUI.java:13)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$174(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$149(Unknown Source)
... 1 more
Caused by: java.io.UncheckedIOException: java.nio.charset.MalformedInputException: Input length = 1
at java.io.BufferedReader$1.hasNext(Unknown Source)
at java.util.Iterator.forEachRemaining(Unknown Source)
at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Unknown Source)
at java.util.stream.ReferencePipeline$Head.forEach(Unknown Source)
at base.PeopleIds.initialize(PeopleIds.java:17)
at base.GUIController.initialize(GUIController.java:36)
... 18 more
Caused by: java.nio.charset.MalformedInputException: Input length = 1
at java.nio.charset.CoderResult.throwException(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.BufferedReader.fill(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
... 24 more
Exception running application base.PersonGUI
I'm not sure what is going on here? I've looked into it and people have said to move the fxml file (the one that is used to format the content and is linked with the GUIController to the same package as the Main class, however it already is.
I've been wrestling with this issue for days to no avail. Do any of you have past experiences with this issue? If so, how did you resolve it? Thanks a lot.
If there is an Exception while the file is being read, not when opening the file, an unchecked exception is thrown for the Files.lines stream operation (Stream.forEach doesn't have a throws clause).
This happens here
Files.lines(Paths.get("res/ids/people_ids.txt")).forEach(s -> {
people.put(s.replaceAll(":.*", "").trim(), Integer.parseInt(s.replaceAll(".*:", "")));
});
, which you can easily see in the stacktrace:
Caused by: java.io.UncheckedIOException: java.nio.charset.MalformedInputException: Input length = 1
(This is caused by the wrong Charset being used, see Files.readAllBytes vs Files.lines getting MalformedInputException )
You don't catch this kind of exception with your catch clause:
} catch (IOException e) {
You need to use
} catch (Exception e) {
to catch the unchecked exceptions too.

How to check if I set CLASSPATH correctly? (Junit error)

I installed Junit and tried to follow one of to tutorial videos on YouTube. I added junit-4.11 to my CLASSPATH correctly but when I run my Junit test, I get initializationError. echo %CLASSPATH% shows the junit-4.11.jar correctly. What could be causing this?
package test;
public class Junit {
public String concatenate(String one, String two)
{
return one + two;
}
public int multiply(int number1, int number2)
{
return number1 * number2;
}
}
package test;
import static org.junit.Assert.*;
import org.junit.Test;
public class MultiplyTest {
#Test
public void testMultiply() {
Junit test = new Junit();
int result = test.multiply(3,4);
assertEquals(12, result);
}
}
java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at org.junit.internal.builders.JUnit4Builder.runnerForClass(JUnit4Builder.java:10)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:26)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:33)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:48)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.ClassNotFoundException: org.hamcrest.SelfDescribing
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 25 more
You need to add hamcrest-all.jar to your classpath also. This question is answered here: java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing

NullPointerException when unmarshalling different attributes types to the same java object

I'm having a serious issue while trying to unmarshal an XML. At first, the Member elements contained only Integer in their "value" attributes (like the first 'EnumType' element), however, now that another 'EnumType' appears with Strings as the value - I'm getting the bellow NullPointerException.
Notice, that the 'value' member in Member.java was from type "int" at the beginning (when only Integer was accepted as values of the Member element) and it worked fine. Only when I have changed it to Object (and I guess this is the source of my issue) - the below exception was appeared.
XML:
<EnumType Name="Genre" UnderlyingType="Edm.Int32">
<Member Name="ACTION" Value="0"/>
<Member Name="COMEDY" Value="1"/>
</EnumType>
<EnumType Name="Rating" UnderlyingType="Edm.String">
<Member Name="RatingA" Value="G"/>
<Member Name="RatingB" Value="PG"/>
</EnumType>
EnumType.java
#XmlRootElement
public class EnumType {
#XmlElement(name = "Member", namespace = "http://schemas.microsoft.com/ado/2009/11/edm")
private List<Member> members = new LinkedList<Member>();
public List<Member> getMembers() {
return members;
}
}
Member.Java
#XmlRootElement
public class Member {
#XmlAttribute(name = "Name")
private String name;
#XmlAttribute(name = "Value")
private Object value;
public String getName() {
return name;
}
public Object getValue() {
return value;
}
}
When unmarshaling the above XML I'm getting this NullPointerException:
java.lang.NullPointerException
at com.sun.xml.internal.bind.v2.runtime.reflect.TransducedAccessor.get(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.property.AttributeProperty.(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.property.PropertyFactory.create(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.property.ArrayElementProperty.(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.property.ArrayElementNodeProperty.(Unknown Source)
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 com.sun.xml.internal.bind.v2.runtime.property.PropertyFactory.create(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.property.ArrayElementProperty.(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.property.ArrayElementNodeProperty.(Unknown Source)
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 com.sun.xml.internal.bind.v2.runtime.property.PropertyFactory.create(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.property.SingleElementNodeProperty.(Unknown Source)
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 com.sun.xml.internal.bind.v2.runtime.property.PropertyFactory.create(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.property.SingleElementNodeProperty.(Unknown Source)
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 com.sun.xml.internal.bind.v2.runtime.property.PropertyFactory.create(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(Unknown Source)
at com.sun.xml.internal.bind.v2.ContextFactory.createContext(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at javax.xml.bind.ContextFinder.newInstance(Unknown Source)
at javax.xml.bind.ContextFinder.newInstance(Unknown Source)
at javax.xml.bind.ContextFinder.find(Unknown Source)
at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
at com.sap.ndb.studio.rdl.csdlparser.jaxb.JAXBParser.load(JAXBParser.java:26)
at com.sap.ndb.studio.rdl.datapreview.functions.LoadGRDL.function(LoadGRDL.java:59)
at org.eclipse.swt.browser.WebSite.Invoke(WebSite.java:773)
at org.eclipse.swt.browser.WebSite$7.method6(WebSite.java:129)
at org.eclipse.swt.internal.ole.win32.COMObject.callback6(COMObject.java:119)
at org.eclipse.swt.internal.win32.OS.DispatchMessageW(Native Method)
at org.eclipse.swt.internal.win32.OS.DispatchMessage(OS.java:2546)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3756)
at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2701)
at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2665)
at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2499)
at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:679)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:668)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:124)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:353)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:180)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:629)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:584)
at org.eclipse.equinox.launcher.Main.run(Main.java:1438)
at org.eclipse.equinox.launcher.Main.main(Main.java:1414)
Any ideas? :(
The JAXB RI will throw that exception when you annotate a field/property of type Object with #XmlAttribute. Like you have in your Member class:
#XmlAttribute(name = "Value")
private Object value;
For More Information
JAXB attribute with Object type throwing null pointer exception?

Categories