Unable to instantiate fragment MainFragment - android-fragments

i'm facing an issue when try to develop a fragemnt and fragment activity simple example.
this is my project code
Mainactivity.java
package com.example.fragment_test;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.example.fragment_test.MainFragment.UiListener;
public class MainActivity extends FragmentActivity implements UiListener{
private MainFragment fragment;
#Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.activity_main);
fragment = (MainFragment) getSupportFragmentManager().findFragmentById(R.id.main_fragment);
}
public void onButtonClicked(){
// handle button clicked
}
}
mainfragment.java
package com.example.fragment_test;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
public class MainFragment extends Fragment {
public interface UiListener{
public void onButtonClicked();
}
private UiListener uiCallback;
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try{
uiCallback = (UiListener) activity; // check if the interface is implemented
}catch(ClassCastException e){
e.printStackTrace();
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main, container, false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.findViewById(R.id.button).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
uiCallback.onButtonClicked();
}
});
}
}
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment android:name="MainFragment"
android:id="#+id/main_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
fragment_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linProgress"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp" >
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
Logcat
06-14 05:06:49.771: I/Process(1563): Sending signal. PID: 1563 SIG: 9
06-14 05:06:50.761: D/AndroidRuntime(1617): Shutting down VM
06-14 05:06:50.761: W/dalvikvm(1617): threadid=1: thread exiting with uncaught exception (group=0xb4b02ba8)
06-14 05:06:50.791: E/AndroidRuntime(1617): FATAL EXCEPTION: main
06-14 05:06:50.791: E/AndroidRuntime(1617): Process: com.example.fragment_test, PID: 1617
06-14 05:06:50.791: E/AndroidRuntime(1617): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fragment_test/com.example.fragment_test.MainActivity}: android.view.InflateException: Binary XML file line #5: Error inflating class fragment
06-14 05:06:50.791: E/AndroidRuntime(1617): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-14 05:06:50.791: E/AndroidRuntime(1617): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-14 05:06:50.791: E/AndroidRuntime(1617): at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-14 05:06:50.791: E/AndroidRuntime(1617): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-14 05:06:50.791: E/AndroidRuntime(1617): at android.os.Handler.dispatchMessage(Handler.java:102)
06-14 05:06:50.791: E/AndroidRuntime(1617): at android.os.Looper.loop(Looper.java:136)
06-14 05:06:50.791: E/AndroidRuntime(1617): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-14 05:06:50.791: E/AndroidRuntime(1617): at java.lang.reflect.Method.invokeNative(Native Method)
06-14 05:06:50.791: E/AndroidRuntime(1617): at java.lang.reflect.Method.invoke(Method.java:515)
06-14 05:06:50.791: E/AndroidRuntime(1617): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-14 05:06:50.791: E/AndroidRuntime(1617): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-14 05:06:50.791: E/AndroidRuntime(1617): at dalvik.system.NativeStart.main(Native Method)
06-14 05:06:50.791: E/AndroidRuntime(1617): Caused by: android.view.InflateException: Binary XML file line #5: Error inflating class fragment
06-14 05:06:50.791: E/AndroidRuntime(1617): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
06-14 05:06:50.791: E/AndroidRuntime(1617): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
06-14 05:06:50.791: E/AndroidRuntime(1617): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
06-14 05:06:50.791: E/AndroidRuntime(1617): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
06-14 05:06:50.791: E/AndroidRuntime(1617): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
06-14 05:06:50.791: E/AndroidRuntime(1617): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
06-14 05:06:50.791: E/AndroidRuntime(1617): at android.app.Activity.setContentView(Activity.java:1929)
06-14 05:06:50.791: E/AndroidRuntime(1617): at com.example.fragment_test.MainActivity.onCreate(MainActivity.java:13)
06-14 05:06:50.791: E/AndroidRuntime(1617): at android.app.Activity.performCreate(Activity.java:5231)
06-14 05:06:50.791: E/AndroidRuntime(1617): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-14 05:06:50.791: E/AndroidRuntime(1617): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-14 05:06:50.791: E/AndroidRuntime(1617): ... 11 more
06-14 05:06:50.791: E/AndroidRuntime(1617): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment MainFragment: make sure class name exists, is public, and has an empty constructor that is public
06-14 05:06:50.791: E/AndroidRuntime(1617): at android.app.Fragment.instantiate(Fragment.java:597)
06-14 05:06:50.791: E/AndroidRuntime(1617): at android.app.Fragment.instantiate(Fragment.java:561)
06-14 05:06:50.791: E/AndroidRuntime(1617): at android.app.Activity.onCreateView(Activity.java:4778)
06-14 05:06:50.791: E/AndroidRuntime(1617): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
06-14 05:06:50.791: E/AndroidRuntime(1617): ... 21 more
06-14 05:06:50.791: E/AndroidRuntime(1617): Caused by: java.lang.ClassNotFoundException: Didn't find class "MainFragment" on path: DexPathList[[zip file "/data/app/com.example.fragment_test-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.fragment_test-1, /system/lib]]
06-14 05:06:50.791: E/AndroidRuntime(1617): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
06-14 05:06:50.791: E/AndroidRuntime(1617): at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
06-14 05:06:50.791: E/AndroidRuntime(1617): at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
06-14 05:06:50.791: E/AndroidRuntime(1617): at android.app.Fragment.instantiate(Fragment.java:583)
06-14 05:06:50.791: E/AndroidRuntime(1617): ... 24 more
06-14 05:06:53.331: I/Process(1617): Sending signal. PID: 1617 SIG: 9
I tried several proposed solution without any success. Would you please help me to run this exmaple

Please give fully qualified name of class in xml file
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="#+id/main_fragment"
android:name="com.example.fragment_test.MainFragment" // UPDATE HERE
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>

Related

JavaFX Gluon Progress Indicator using charm-glisten.jar For Desktop Application

I'm Trying to create a desktop application with circular progress indicator upon using gluon circular progress indicator using charm-glisten.jar file in scene builder I get InvocationTargetException while on using standard javafx-10 progress indicator it shows no Exception any suggestions ?.
Java Code:
package application;
import com.gluonhq.charm.glisten.control.ProgressIndicator;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
public class Main extends Application
{
private double xOffset = 0;
private double yOffset = 0;
#FXML
ProgressIndicator pg;
public void start(Stage primaryStage)
{
try
{
primaryStage.initStyle(StageStyle.TRANSPARENT);
AnchorPane root = FXMLLoader.load(getClass().getResource("Test1.fxml"));
Scene scene = new Scene(root,600,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
scene.setFill(javafx.scene.paint.Color.TRANSPARENT);
primaryStage.setScene(scene);
primaryStage.show();
root.setOnMousePressed(new EventHandler<MouseEvent>()
{
public void handle(MouseEvent e)
{
xOffset = e.getSceneX();
yOffset = e.getSceneY();
}
});
root.setOnMouseDragged(new EventHandler<MouseEvent>()
{
public void handle(MouseEvent e)
{
primaryStage.setX(e.getScreenX() - xOffset);
primaryStage.setY(e.getScreenY() - yOffset);
}
});
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
launch(args);
}
}
FXML Code:
<?xml version="1.0" encoding="UTF-8"?>
<?import com.gluonhq.charm.glisten.control.ProgressIndicator?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" style="-fx-background-color: #3D4956;" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Main">
<children>
<ProgressIndicator fx:id="pg" layoutX="196.0" layoutY="86.0" radius="100.0" />
</children>
</AnchorPane>
Error:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:830)
Caused by: java.lang.NoClassDefFoundError: com/sun/javafx/css/converters/SizeConverter
at com.gluonhq.charm.glisten.control.ProgressIndicator.<clinit>(SourceFile:81)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
at java.base/java.lang.reflect.ReflectAccess.newInstance(ReflectAccess.java:166)
at java.base/jdk.internal.reflect.ReflectionFactory.newInstance(ReflectionFactory.java:404)
at java.base/java.lang.Class.newInstance(Class.java:591)
at javafx.fxml/javafx.fxml.FXMLLoader$InstanceDeclarationElement.constructValue(FXMLLoader.java:1019)
at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:754)
at javafx.fxml/javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2722)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3237)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106)
at application.Main.start(Main.java:30)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
... 1 more
Caused by: java.lang.ClassNotFoundException: com.sun.javafx.css.converters.SizeConverter
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 29 more
Exception running application application.Main
enter image description here

Error in validation of JFXTextField

This is my first JavaFX program and I want to accept the username via eUser and password via ePass. When I try to check if the textfield is empty using validate I get this error:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: javafx.fxml.LoadException:
file:/Users/dylan/NetBeansProjects/JavaFXApplication2/dist/run350595374/JavaFXApplication2.jar!/javafxapplication2/FXMLDocument.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at javafxapplication2.JavaFXApplication2.start(JavaFXApplication2.java:23)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Caused by: java.lang.NullPointerException
at javafxapplication2.FXMLDocumentController.initialize(FXMLDocumentController.java:48)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
... 14 more
Exception running application javafxapplication2.JavaFXApplication2
Java Result: 1
When I comment out eUser.getValidators().add(vali); from Controller.java file and the lines that follow it, then no errors show up.
This is my FXMLDocumentController.java file:
public class FXMLDocumentController implements Initializable {
#FXML
private JFXTextField eUser;
#FXML
private JFXTextField ePass;
#FXML
private void handleClose(MouseEvent event) {
System.exit(0);
}
#Override
public void initialize(URL url, ResourceBundle rb) {
RequiredFieldValidator vali = new RequiredFieldValidator();
eUser.getValidators().add(vali);
vali.setMessage("No Input Given");
vali.focusedProperty().addListener(new ChangeListener<Boolean>(){
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
if(!newValue)
{
eUser.validate();
}
}});}}
Main class:
public class JavaFXApplication2 extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
stage.initStyle(StageStyle.UNDECORATED);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Maybe this could be the problem. In your FXML file, check if you set those ID's to the relevant textfields. That might possibly be the thing which is causing the NullPointerException.
If you're using SceneBuilder, click on the relevant textfield -> Code and set the fx:id of those textFields.
And by the way, you can probably use a PasswordField instead of a TextField :)

NullPointerException when trying to execute DAO... Could not initialize class ..JavaReflectionManager

I am getting a NullPointerException when I try to execute the checkCredentials function in my LoginController. I think the problem is not with my Service Implementation but with DAO Implementation. But I am not able to find what causes the issue. I am not sure if the problem is with the
#Autowired
private LoginDAO loginDAO;
or the
#Repository("loginDAO")
When I submit the login credentials I get the following
DEBUG - Returning handler method [public java.lang.String com.accademics.web.controllers.LoginController.checkCredentials(com.accademics.web.models.LoginModel,org.springframework.validation.BindingResult)]
DEBUG - Returning cached instance of singleton bean 'loginController'
inside controller checkCredentials
LoginModel [username=admin, password=password]
service checking login of admin and password
DEBUG - Resolving exception from handler [public java.lang.String com.accademics.web.controllers.LoginController.checkCredentials(com.accademics.web.models.LoginModel,org.springframework.validation.BindingResult)]: java.lang.NullPointerException
DEBUG - Resolving exception from handler [public java.lang.String com.accademics.web.controllers.LoginController.checkCredentials(com.accademics.web.models.LoginModel,org.springframework.validation.BindingResult)]: java.lang.NullPointerException
DEBUG - Resolving exception from handler [public java.lang.String com.accademics.web.controllers.LoginController.checkCredentials(com.accademics.web.models.LoginModel,org.springframework.validation.BindingResult)]: java.lang.NullPointerException
DEBUG - Could not complete request
java.lang.NullPointerException
at com.accademics.data.services.LoginServiceImpl.checkCredentials(LoginServiceImpl.java:18)
at com.accademics.web.controllers.LoginController.checkCredentials(LoginController.java:39)
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.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:116)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:108)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:784)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:802)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1410)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
So here is the root, applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
<bean id="loginService" class="com.accademics.data.services.LoginServiceImpl"/>
</beans>
The dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="com.accademics.web.controllers"></context:component-scan>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages"></property>
</bean>
<mvc:resources location="/resources/" mapping="/resources/**"></mvc:resources>
</beans>
student_login.jsp actual content part
<form action='<spring:url value="/student/login"></spring:url>' method="post" autocomplete="on">
<h1>Log in</h1>
<p>
<label for></label>
</p>
<p>
<label for="username" class="uname" > Username </label>
<input id="username" name="username" required="required" type="text" placeholder="Username"/>
</p>
<p>
<label for="password" class="passwd"> Password </label>
<input id="password" name="password" required="required" type="password" placeholder="Password" />
</p>
<p class="keeplogin">
<input type="checkbox" name="saveLogin" id="saveLogin" value="saveLogin" />
<label for="saveLogin">Keep me logged in</label>
</p>
<p class="login button">
<input type="submit" value="Login" />
</p>
</form>
LoginController.java
package com.accademics.web.controllers;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.accademics.data.services.LoginService;
import com.accademics.web.models.LoginModel;
#Controller
#RequestMapping("/student")
public class LoginController {
#Autowired
public LoginService loginService;
#Autowired
MessageSource messagesource;
#RequestMapping(value="/login", method=RequestMethod.GET)
public String displayLogin(Model model){
return "student_login";
}
#RequestMapping(value="/login", method=RequestMethod.POST)
public String checkCredentials(#Valid #ModelAttribute LoginModel loginModel, BindingResult result){
System.out.println("inside controller checkCredentials");
if(result.hasErrors()){
return "student_login";
}
System.out.println(loginModel);
boolean userExists = loginService.checkCredentials(loginModel.getUsername(), loginModel.getPassword());
if(userExists){
System.out.println("valid");
return "dashboard";
}else{
System.out.println("invalid");
result.rejectValue("userName", "NotNull.login.invalidUser");
return "student_login";
}
}
}
LoginServiceImpl.java
package com.accademics.data.services;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.accademics.web.DAO.LoginDAO;
#Service("loginService")
public class LoginServiceImpl implements LoginService {
#Autowired
private LoginDAO loginDAO;
#Override
public boolean checkCredentials(String username, String password) {
// TODO Auto-generated method stub
System.out.println("service checking login of "+username+" and "+password);
return loginDAO.checkCredentials(username, password);
}
}
LoginDAO.java
package com.accademics.web.DAO;
public interface LoginDAO {
public boolean checkCredentials(String username, String password);
}
LoginDAOImpl.java
package com.accademics.web.DAO;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
#Repository("loginDAO")
public class LoginDAOImpl implements LoginDAO {
#Autowired
private SessionFactory sessionFactory;
protected Session getSession(){
return sessionFactory.getCurrentSession();
}
#Override
public boolean checkCredentials(String username, String password) {
System.out.println("Checking login");
//HQL
String hquery = "from student_credentials where student_credentials.username=? and student_credentials.password=?";
boolean userFound = false;
Session session = getSession();
Query query = session.createSQLQuery(hquery);
query.setParameter(0, username);
query.setParameter(1, password);
List list = query.list();
if(!list.isEmpty()){
userFound = true;
}
session.close();
return userFound;
}
}
and relevant entry in messages.properties
NotNull.login.invalidUser = Invalid Username/Password
Please help me to resolve this issue.
I have made the following changes to my dispatcher-servlet and removed the bean tag from applicationContext.xml,
<?xml version="1.0" encoding="UTF-8"?>
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="com.accademics.web" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages"></property>
</bean>
<mvc:resources location="/resources/" mapping="/resources/**"></mvc:resources>
which created this new exception.
ERROR - Context initialization failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'loginController': Unsatisfied dependency expressed through field 'loginService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'loginService': Unsatisfied dependency expressed through field 'loginDAO'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'loginDAO': Unsatisfied dependency expressed through field 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in com.accademics.web.configuration.HibernateConfiguration: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.hibernate.annotations.common.reflection.java.JavaReflectionManager
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:668)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:634)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:682)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:553)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:494)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:138)
at javax.servlet.GenericServlet.init(GenericServlet.java:158)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1183)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1099)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:779)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:133)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:108)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:784)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:802)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1410)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'loginService': Unsatisfied dependency expressed through field 'loginDAO'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'loginDAO': Unsatisfied dependency expressed through field 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in com.accademics.web.configuration.HibernateConfiguration: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.hibernate.annotations.common.reflection.java.JavaReflectionManager
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
... 39 more
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'loginDAO': Unsatisfied dependency expressed through field 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in com.accademics.web.configuration.HibernateConfiguration: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.hibernate.annotations.common.reflection.java.JavaReflectionManager
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
... 52 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in com.accademics.web.configuration.HibernateConfiguration: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.hibernate.annotations.common.reflection.java.JavaReflectionManager
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
... 65 more
Caused by: java.lang.NoClassDefFoundError: Could not initialize class org.hibernate.annotations.common.reflection.java.JavaReflectionManager
at org.hibernate.cfg.Configuration.createReflectionManager(Configuration.java:2547)
at org.hibernate.cfg.Configuration.createReflectionManager(Configuration.java:2543)
at org.hibernate.cfg.Configuration.reset(Configuration.java:298)
at org.hibernate.cfg.Configuration.<init>(Configuration.java:289)
at org.hibernate.cfg.Configuration.<init>(Configuration.java:293)
at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.<init>(LocalSessionFactoryBuilder.java:140)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:343)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)
... 75 more
I really dont understand what is wrong with my code.
I now have the solution, which is decribed in the below answer. The second problem was actually related to dependencies.
I found the solution to the second issue (first which was solved Thanks to #JBNizet)
It was actually regarding the dependencies added in the pom.xml, which I havnt included here. What I did is I actually upgraded my hibernate version to 5.1.0 and then added hibernate-commons-annotations 5.0.1.Final. So my dependencies are now like below.
The specific, Hibernate core will have hibernate-common-annotation and this has the missing class
Caused by: java.lang.NoClassDefFoundError: Could not initialize class org.hibernate.annotations.common.reflection.java.JavaReflectionManager

JavaFX Location is required NullPointerException Netbeans

So I tried building a JavaFX application but I get a NullPointerException "Location is required". I already found a couple of questions regarding this but none of the answers worked for me.
I already tried:
Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("FXMLDocument.fxml"));
Parent root = FXMLLoader.load(getClass().getResource("/FXMLDocument.fxml"));
And moving FXMLDocument.fxml to default package but none of this worked for me.
This is the untouched JavaFX FXML Application created by Netbeans, I really don't understand why this doesn't work out of the box...
Thanks in advance!
Project/File Structure:
Test.java:
package test;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* #author Tim
*/
public class Test extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
FXMLDocument.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" fx:controller="test.FXMLDocumentController">
<children>
<Button layoutX="126" layoutY="90" text="Click Me!" onAction="#handleButtonAction" fx:id="button" />
<Label layoutX="126" layoutY="120" minHeight="16" minWidth="69" fx:id="label" />
</children>
</AnchorPane>
FXMLDocumentController.java:
package test;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
/**
*
* #author Tim
*/
public class FXMLDocumentController implements Initializable {
#FXML
private Label label;
#FXML
private void handleButtonAction(ActionEvent event) {
System.out.println("You clicked me!");
label.setText("Hello World!");
}
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
Stacktrace:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$156(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at test.Test.start(Test.java:22)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$149(WinApplication.java:191)
... 1 more
Exception running application test.Test
#James_D was right, the project needed to be cleaned and rebuilt (Shift+F11) (even though I don't know why since I created the project from scratch via the Netbeans FXML Application template..)
If the method doesn't work at all you have to Clean and build your project. That's worked for me
I had the same problem and could solve it. My answer is here JavaFX NullPointerException Location is required NetBeans . In netbeans 8.2 in the project properties under Build > Deployment there is a check-box "Request unrestricted access (Enable signing)".

I am getting NullPointerException in my Spring MVC project?

I have a simple Library project based on spring MVC framework. I am going to add a new author to my author table but I am getting NullPointerException.
I provided add classes related to this function below. can anybody say what is wrong?
#Controller
public class AuthorController {
#RequestMapping("/addAuthor")
protected ModelAndView addAuthor() throws Exception {
ModelAndView model = new ModelAndView("addAuthor");
return model;
}
#RequestMapping("/addAuthorExecution")
protected ModelAndView addAuthorExecution(
#RequestParam("authorName") String authorName) throws Exception {
ApplicationContext cnx = new ClassPathXmlApplicationContext("spring.xml");
AuthorService authorService = (AuthorService) cnx.getBean("authorService");
authorService.addAuthor(authorName);
ModelAndView model = new ModelAndView("adminFunctionsPage");
model.addObject("successMsg", "Your request has been processed Successfully.");
return model;
}
}
Service Class:
public class AuthorService {
#Autowired
Author author;
#Autowired
AuthorDAO authorDAO;
public void addAuthor(String authorName) throws ClassNotFoundException,
SQLException {
AuthorDAO authorDAO = new AuthorDAO();
authorDAO.addAuthor(authorName);
}
}
DAO class:
public class AuthorDAO extends JdbcDaoSupport {
public void addAuthor(String authorName) {
String sql = "insert into tbl_author (authorName) values (?)";
this.getJdbcTemplate().update(sql, new Object[] { authorName });
}
private static final class AuthorMapper implements RowMapper<Author> {
#Autowired
Author author;
#Override
public Author mapRow(ResultSet rs, int rowNum) throws SQLException {
author.setAuthorId(rs.getInt("authorId"));
author.setAuthorName(rs.getString("authorName"));
return author;
}
}
}
Model Class:
public class Author {
int authorId;
String authorName;
public int getAuthorId() {
return authorId;
}
public void setAuthorId(int authorId) {
this.authorId = authorId;
}
public String getAuthorName() {
return authorName;
}
public void setAuthorName(String authorName) {
this.authorName = authorName;
}
}
XML:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.hojat.LibSpringMVCProject.DAO" />
<context:component-scan base-package="com.hojat.LibSpringMVCProject.Service" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/library"/>
<property name="username" value="root"/>
<property name="password" value="1234"/>
</bean>
<bean id="author" class="com.hojat.LibSpringMVCProject.model.Author"/>
<bean id="authorDAO" class="com.hojat.LibSpringMVCProject.DAO.AuthorDAO" >
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="authorService" class="com.hojat.LibSpringMVCProject.service.AuthorService"/>
Error:
'INFO: Loaded JDBC driver: com.mysql.jdbc.Driver
Feb 24, 2015 1:16:11 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [spring-dispatcher] in context with path [/LibrarySpringMVCProject] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
at com.hojat.LibSpringMVCProject.DAO.AuthorDAO.addAuthor(AuthorDAO.java:32)
at com.hojat.LibSpringMVCProject.service.AuthorService.addAuthor(AuthorService.java:40)
at com.hojat.LibSpringMVCProject.controller.AuthorController.addAuthorExecution(AuthorController.java:46)
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.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:777)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:706)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:537)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1085)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:146)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:277)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)'
Hello could you try
<property name="url" value="jdbc:mysql://localhost:3306/library"/>
instead of
<property name="url" value="jdbc:mysql://localhost/library"/>
It will be problem.
Several mistakes
You are autowiring the POJO entity class Auther. This is wrong.
Hope you have #Service annotation on the AuthorService
You are creating an object of the the AuthorDAO inside the addAuthor method. You should note the AutherDAO should not be manually constructed as there are autowired object available inside that class AutherDAO.AuthorMapper. By manually constructing them these auto wiring will not get wired to objects.
If I completely rewrite your service class, it would be something like this.
#Service
public class AuthorService {
#Autowired
AuthorDAO authorDAO;
public void addAuthor(String authorName) throws Exception {
authorDAO.addAuthor(authorName);
}
}

Resources