Showing posts with label IllegalStateException. Show all posts
Showing posts with label IllegalStateException. Show all posts

Sunday, May 8, 2016

java.lang.IllegalStateException: An Errors/BindingResult argument is expected to be declared immediately after the model attribute

By default, Spring MVC throws an exception when errors occur during request binding. This usually not what we want – instead we should be presenting these errors to the user. We’re going to use a BindingResult by adding one as an argument to our controller method:

 @RequestMapping(value="/addEmployee", method=RequestMethod.POST)  
      public String addEmployees(BindingResult result, @ModelAttribute("employee") Employee employee,  
                Model model, final RedirectAttributes redirectAttributes){  

The BindingResult argument needs to be positioned right after our form backing object – it’s one of the rare cases where the order of the method arguments matters. Otherwise we’ll run into the following exception :


java.lang.IllegalStateException: An Errors/BindingResult argument is expected to be declared immediately after the model attribute, the @RequestBody or the @RequestPart arguments to which they apply:
public java.lang.String controller.EmployeeController.addEmployees(org.springframework.validation.BindingResult,entity.Employee,org.springframework.ui.Model,org.springframework.web.servlet.mvc.support.RedirectAttributes)


To correct the above exception, just change order of the parameter, where your binding result parameter has to be just after the ModelAttribute as below

      @RequestMapping(value="/addEmployee", method=RequestMethod.POST)  
      public String addEmployees( @ModelAttribute("employee") Employee employee,  
                BindingResult result, Model model, final RedirectAttributes redirectAttributes){  


Happy Programming...!!!

Saturday, April 19, 2014

java.lang.IllegalStateException: ArrayAdapter requires the resources ID to be a TextView

Recently i was creating ListActivity for my Android application. After doing the necessary work, when i try to run the application, i was spatted with the error java.lang.IllegalStateException: ArrayAdapter requires the resources ID to be a TextView. What causes this issue?.

When you are passing an layout to the constructor like this new ArrayAdapter(this, R.layout.list_employee, this.employeeList). You need to make sure that your layout file list_employee wasn't wrapped by another layout. Unfortunately, my layout was wrapped with the RelativeLayout , which results in the above exception.

Below is the layout of my activity, which results in the exception

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   android:paddingBottom="@dimen/activity_vertical_margin"  
   android:paddingLeft="@dimen/activity_horizontal_margin"  
   android:paddingRight="@dimen/activity_horizontal_margin"  
   android:paddingTop="@dimen/activity_vertical_margin"  
  >  
   <TextView  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="@string/employee_list_heading" />  
 </RelativeLayout>  

To Resolve the issue, i have just removed the layout element which just wrapped my TextView, and modified my XML as below

 <?xml version="1.0" encoding="utf-8"?>  
 <TextView xmlns:android="http://schemas.android.com/apk/res/android"  
   android:layout_width="fill_parent"  
   android:layout_height="wrap_content"   
   // other attributes of the TextView  
 />  

Happy Programming...!!!