Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8719337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:46:14+00:00 2026-06-13T06:46:14+00:00

I am using play 1.2.5. I am sending a JSON object from a view

  • 0

I am using play 1.2.5. I am sending a JSON object from a view via ajax, but I get a Null Pointer Exception in my controller. Below is my code. It take values from a Html field and sends them to the Play controller.

View

function submitData(){
                'use strict';
                var user = document.getElementById('user').value;
                var pass = document.getElementById('pass').value;

                var message = "";
                if(user == "" && pass==""){
                    //message=null;
                    message = "Please Enter User and Password";
                    document.getElementById('submitb').disabled=true;
                }else if(user === ""){
                    //message= null;
                    message = " Please Enter User name";
                    document.getElementById('submitb').disabled=true;
                }else if(pass == ""){
                    //message=null;
                    message = "Please Enter Password";
                    document.getElementById('submitb').disabled=true;
                }

                if(message!=""){
                    document.getElementById('result').innerHTML = message;
                    document.getElementById('submitb').disabled= false;
                    message = null;
                }else{
                    sendAjax();
                }


            } // ending submit function

SendAjax Function:

function sendAjax(){

                'use strict';
                var ajax = getXhrObject();
                ajax.onreadystatechange = handleAjaxResponse;
                    //var userData =  
                    var data = {'user': document.getElementById('user').value,'pass': document.getElementById('pass').value}
                    var jsonData = JSON.stringify(data);

                    ajax.open('POST', '/validate', true);
                    ajax.setRequestHeader('contentType', 'application/json');
                    var ajaxAbortTimer = setTimeout(function() { //abort timer
                        if (ajax) {
                                ajax.abort();
                                ajax = null;
                        } // second 'if (ajax)' ends here
                    }, 5000);
                    ajax.send(jsonData);







            }// ending sendAjax function.

getXhrObject()

function getXhrObject(){
                'use strict';
                var ajax = null; 
                try{
                    if(window.XMLHttpRequest){
                        ajax = new XMLHttpRequest();
                    }else if(window.ActiveXObject){
                        ajax = new ActiveXObject();
                    }



                }catch(e){
                    console.log(e);
                }

                return ajax;

            } // ending getXhrObject Function

Controller:
Here is the code of my controller. When I send a request to it, a null pointer exception is thrown.

public static void validate(Request request){
        String status = null, response = null;
        List<String> credInfo = new ArrayList<String>();
        if (request.contentType.equals("application/json")){
            Iterator<String> it = request.params.allSimple().keySet().iterator();
            while (it.hasNext()) {
                String key = it.next();
                String value = request.params.get(key);
                Logger.info("value" + value);
                JsonElement body = new JsonParser().parse(value);
                JsonObject jsObj = body.getAsJsonObject();
                JsonElement JsonEmail = jsObj.get("user");
                JsonElement JsonPassword = jsObj.get("pass");
                String email = JsonEmail.getAsString();
                String password = JsonPassword.getAsString();
                System.out.println("email:  "+email);
                System.out.println("password:"+password);
                if(!email.equals(user) && !password.equals(pass)){

                status = "fail"; response = "Authentication failed";

            }
        }

    }
        renderJSON("{\"status\":"+status+" \response\":"+response+" }");

  }

The stacktrace for the exception is:

Internal Server Error (500) for request POST /validate

Execution exception (In /app/controllers/Application.java around line 30)
NullPointerException occured : Try to read contentType on null object play.mvc.Http$Request (controllers.Application.validate, line 30)

play.exceptions.JavaExecutionException: Try to read contentType on null object play.mvc.Http$Request (controllers.Application.validate, line 30)
    at play.mvc.ActionInvoker.invoke(ActionInvoker.java:237)
    at Invocation.HTTP Request(Play!)
Caused by: java.lang.NullPointerException: Try to read contentType on null object play.mvc.Http$Request (controllers.Application.validate, line 30)
    at play.classloading.enhancers.PropertiesEnhancer$FieldAccessor.invokeReadProperty(PropertiesEnhancer.java:213)
    at controllers.Application.validate(Application.java:30)
    at play.mvc.ActionInvoker.invokeWithContinuation(ActionInvoker.java:557)
    at play.mvc.ActionInvoker.invoke(ActionInvoker.java:508)
    at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:484)
    at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:479)
    at play.mvc.ActionInvoker.invoke(ActionInvoker.java:161)
    ... 1 more
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T06:46:16+00:00Added an answer on June 13, 2026 at 6:46 am

    You don’t use play framework the right way : http://www.playframework.org/documentation/1.2.5/controllers#params

    Controller method parameters match the name of the parameters you send. The http request is not a parameter so you must not use a parameter like this. The request object is already defined. Try to remove this parameter from your method.

    Another problem is that you do not give a name to your json object in your ajax call so you can’t retrieve it with something like request.params.get(“user”)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Play 2.0.1 I defined the following route: GET /demo/list controllers.Demos.listDemos(page: Int ?= 0,
I am sending a JSON message via XHR in a post request (content type:
I'm sending the following json message via POST request: { ID:17, FIRST_NAME:John, LAST_NAME:Doe, TOKEN:8cdde0ef552e305cb44e143d3f8c742fb35caab8f360e7
Using Play! Framework 2.0.2, when I add several items from my java project to
I`m using Play! Framework 2.0 and I'm new in this framework. How can I
I have been using play 1.2.5rc4 for development of one app and I have
We are using play 1.2.x DB evolutions feature. I want to have the scripts
I am using Play Framework 2.0 with Scala. So I have an action that
I'm using play framework 1.2.4 and i'm preparing an meal order system. So i
I am writing a web-application using Play 1.2.3 . One of the feature is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.