NullPointerException Rowselect datatable

UI Components for JSF
Post Reply
prime2000
Posts: 31
Joined: 26 Oct 2011, 05:17

30 Oct 2011, 19:30

Hi,

Primefaces team.

I chose primefaces for a reason over Richfaces. The primefaces approach was easy to understand and compact for some of the functionalities and component implementation.

I am not sure if the primefaces showcase for 3.0 examples are intentially incomplete or a mistake for missing parts of the code, but it seems like every example I try, I run into issues.

One example I am trying to implement is the Datatable Checkbox selection my post unanswered (viewtopic.php?f=3&t=15853)

or

the single rowselection below

I keep running into nullpointer exceptions.

I am using 3.0.M3 AND TOMCAT 6.

Example site
http://www.primefaces.org/showcase-labs ... nstant.jsf

error

Code: Select all

Caused by: java.lang.NullPointerException
	at com.jpmc.cisd.TableBean.onRowSelect(TableBean.java:119)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.apache.el.parser.AstValue.invoke(AstValue.java:191)
	... 23 more
TableBean.java

Code: Select all


import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import javax.enterprise.context.SessionScoped;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;

import org.primefaces.event.SelectEvent;
import org.primefaces.event.UnselectEvent;

@ManagedBean
@SessionScoped
public class TableBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6839748907567487131L;

	private final static String[] colors;

	private final static String[] manufacturers;

	static {
		colors = new String[10];
		colors[0] = "Black";
		colors[1] = "White";
		colors[2] = "Green";
		colors[3] = "Red";
		colors[4] = "Blue";
		colors[5] = "Orange";
		colors[6] = "Silver";
		colors[7] = "Yellow";
		colors[8] = "Brown";
		colors[9] = "Maroon";

		manufacturers = new String[10];
		manufacturers[0] = "Mercedes";
		manufacturers[1] = "BMW";
		manufacturers[2] = "Volvo";
		manufacturers[3] = "Audi";
		manufacturers[4] = "Renault";
		manufacturers[5] = "Opel";
		manufacturers[6] = "Volkswagen";
		manufacturers[7] = "Chrysler";
		manufacturers[8] = "Ferrari";
		manufacturers[9] = "Ford";
	}

	private List<Car> cars;

	private Car selectedCar;

	private Car[] selectedCars;

	private CarDataModel mediumCarsModel;

	public TableBean() {
		cars = new ArrayList<Car>();

		populateRandomCars(cars, 500);

		mediumCarsModel = new CarDataModel(cars);
	}

	public Car[] getSelectedCars() {
		return selectedCars;
	}

	public void setSelectedCars(Car[] selectedCars) {
		this.selectedCars = selectedCars;
	}

	public Car getSelectedCar() {
		if (selectedCar != null)
			System.out.println("Selected Car is...: " + selectedCar);
		return selectedCar;
	}

	public void setSelectedCar(Car selectedCar) {
		this.selectedCar = selectedCar;
	}

	private void populateRandomCars(List<Car> list, int size) {
		for (int i = 0; i < size; i++)
			list.add(new Car(getRandomModel(), getRandomYear(),
					getRandomManufacturer(), getRandomColor()));
	}

	private int getRandomYear() {
		return (int) (Math.random() * 50 + 1960);
	}

	private String getRandomColor() {
		return colors[(int) (Math.random() * 10)];
	}

	private String getRandomManufacturer() {
		return manufacturers[(int) (Math.random() * 10)];
	}

	private String getRandomModel() {
		return UUID.randomUUID().toString().substring(0, 8);
	}

	public CarDataModel getMediumCarsModel() {
		return mediumCarsModel;
	}

	public void onRowSelect(SelectEvent event) {
		
		
		FacesMessage msg = new FacesMessage("Car Selected",
				((Car) event.getObject()).getModel());

		FacesContext.getCurrentInstance().addMessage(null, msg);
	}

	public void onRowUnselect(UnselectEvent event) {
		FacesMessage msg = new FacesMessage("Car Unselected",
				((Car) event.getObject()).getModel());

		FacesContext.getCurrentInstance().addMessage(null, msg);
	}

	public String onRowSelectNavigate(SelectEvent event) {
		FacesContext.getCurrentInstance().getExternalContext().getFlash()
				.put("selectedCar", event.getObject());

		return "carDetail?faces-redirect=true";
	}

}

datatable.xhtml

Code: Select all

<html xmlns="http://www.w3c.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.prime.com.tr/ui">
<h:head>
       
</h:head>
<h:body>

<h:form id="form">

    <p:growl id="growl" showDetail="true"/>

    <p:dataTable var="car" value="#{tableBean.mediumCarsModel}" paginator="true" rows="10" selection="#{tableBean.selectedCar}" selectionMode="single">

        <p:ajax event="rowSelect" listener="#{tableBean.onRowSelect}" update=":form:display :form:growl" oncomplete="carDialog.show()"/>
        
        <p:ajax event="rowUnselect" listener="#{tableBean.onRowUnselect}" update=":form:growl"/>

        <f:facet name="header">
            Select a row to display a message
        </f:facet>

        <p:column headerText="Model">
            #{car.model}
        </p:column>

        <p:column headerText="Year">
            #{car.year}
        </p:column>

        <p:column headerText="Manufacturer" >
            #{car.manufacturer}
        </p:column>

        <p:column headerText="Color">
            #{car.color}
        </p:column>
    </p:dataTable>

    <p:dialog header="Car Detail" widgetVar="carDialog" resizable="false" showEffect="explode" hideEffect="explode">

        <h:panelGrid id="display" columns="2" cellpadding="4">

            <f:facet name="header">
                <p:graphicImage value="/images/cars/#{tableBean.selectedCar.manufacturer}.jpg"/>
            </f:facet>

            <h:outputText value="Model:" />
            <h:outputText value="#{tableBean.selectedCar.model}" />

            <h:outputText value="Year:" />
            <h:outputText value="#{tableBean.selectedCar.year}" />

            <h:outputText value="Manufacturer:" />
            <h:outputText value="#{tableBean.selectedCar.manufacturer}" />

            <h:outputText value="Color:" />
            <h:outputText value="#{tableBean.selectedCar.color}" />
            
        </h:panelGrid>
    </p:dialog>

</h:form>
                    
                               
                    
</h:body>
</html>  
What is the issue with the code above.

Please help.

Thanks

tandraschko
PrimeFaces Core Developer
Posts: 3979
Joined: 03 Dec 2010, 14:11
Location: Bavaria, DE
Contact:

30 Oct 2011, 20:37

The NPE occurs in your own method. Did you debugged your method?
Thomas Andraschko

PrimeFaces | PrimeFaces Extensions

Apache Member | OpenWebBeans, DeltaSpike, MyFaces, BVal, TomEE

Sponsor me: https://github.com/sponsors/tandraschko
Blog: http://tandraschko.blogspot.de/
Twitter: https://twitter.com/TAndraschko

prime2000
Posts: 31
Joined: 26 Oct 2011, 05:17

31 Oct 2011, 00:31

Hi,

The method is the showcase method.

Code: Select all

 public void onRowSelect(SelectEvent event) {
      
      
      FacesMessage msg = new FacesMessage("Car Selected",
            ((Car) event.getObject()).getModel());

      FacesContext.getCurrentInstance().addMessage(null, msg);
   }

The line... ((Car) event.getObject()).getModel()); returns null when it should return the selected row data.

pmbtgun@gmail.com
Posts: 7
Joined: 14 Dec 2015, 17:54

14 Apr 2016, 21:46

Hey, what happend with that?

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 39 guests