ironmantis7x
ironmantis7x

Reputation: 827

Grails error Null Pointer Exception accesses GSP

Hey Guys: I am really stuck on this one.

I have a grails app that is using a REST service. It works fine for accessing the REST service, but the issue is when I want to render the results in a GSP. I get the following error:

errors.GrailsExceptionResolver  - NullPointerException occurred when processing request: [GET] /TestAPI/login/index
Stacktrace follows:
java.lang.NullPointerException
    at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:198)
    at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

Here are my issues: 1. I am trying to send the output to a different GSP and not index. 2. I read the stack trace and in my project, I have index.gsp in that path that it says it can't find.

Here is my controller code:

package testapi

import org.springframework.http.HttpEntity
import org.springframework.http.HttpHeaders
import org.springframework.http.client.support.HttpAccessor
import org.springframework.web.client.RestTemplate
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;

import groovyx.net.http.*

import java.util.Base64;
import java.util.Map;


//import org.springframework.web.client.RestTemplate;

class LoginController {

    def index() 
    { 
        String usernm = "xxxxxxxxxx";
        String link = "https://xxx.xxx.xxx/oauth2/token";
        String passwd = "yyyyyy";
        //Base64.encodeBase64(usernm.concat(":".concat(passwd)).getBytes())
        Base64.getEncoder().encode(usernm.concat(":".concat(passwd)).getBytes());
        String base64UserCreds = new String(Base64.getEncoder().encode(usernm.concat(":".concat(passwd)).getBytes()));

        RestTemplate restTemplate = new RestTemplate();

        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.add("Authorization","Basic ".concat(base64UserCreds));

        HttpEntity<String> request = new HttpEntity<>(requestHeaders);

        System.out.println ("URI under Test is: " + String.valueOf(link) + ". \n" + "Please stand by for results ... \n \n ");
        System.out.println ("Base 64 encoded string is: " + base64UserCreds);

        @SuppressWarnings("rawtypes")
        Map result = restTemplate.exchange(link, HttpMethod.POST, request, Map.class).getBody();

        String authtype = (String) result.get("tokenType");
        String tokenaccess = (String) result.get("accessToken");
        //PrintStream testOutput = null;
        //testOutput.append(result2);
        System.out.println("Access token: " + tokenaccess + "\n");

        HttpHeaders requestHeaders2 = new HttpHeaders();
        requestHeaders2.add("zzzz-pppp-oauth-scope", "zzz:read-any");
        requestHeaders2.add("Authorization","Bearer ".concat(tokenaccess));
        HttpEntity<String> request2 = new HttpEntity<>(requestHeaders2);
        System.out.println("URI #1 under test: https://xxxx.xxxx.xxxx?expand=reps \n");
        String result3 = restTemplate.exchange("https://xxxx.xxxx.xxxx?expand=reps", HttpMethod.GET, request2, String.class).getBody();
        [result3]
        render(view: "api_test", model:[name: result3])
        System.out.println(result3);

    }
}

And here is the gsp:

<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="layout" content="main"/>
<title>AutoTest - API</title>
</head>
<body>
  <div class="body">

  API Results ${result3}

  </div>
</body>
</html>

NOTE: I do not have a domain class file; Do I need one if so - please advise me how to structure it.

Thanks.

NOTE: adding a picture of the directory structure of the project:

grails project directory structure

Upvotes: 0

Views: 975

Answers (1)

Joeri Landman
Joeri Landman

Reputation: 56

render result3 will not render a view but the content of result3, so the json response is not weird.

if you could post your filenames with directories from 'grails-app' we might see the problem (e.g. '/grails-app/views/login/api_test.gsp')

Upvotes: 1

Related Questions