0

Im trying to fix bugs, but i just cant find how to solve this part:

This is the error. The program runs a bit, then in throws null-pointer

Properties value: DEVELOPEMENT
App is running...
Exception in thread "main" java.lang.NullPointerException
    at hu.citec.hazi.hazi.service.Service.print(Service.java:12)
    at hu.citec.hazi.hazi.Main.run(Main.java:26)
    at hu.citec.hazi.hazi.Main.main(Main.java:20)

my code: Repo class:

package hu.citec.hazi.hazi.repo;

import java.time.LocalDateTime;

@org.springframework.stereotype.Repository
public class RepoImpl implements Repository {

    public String getPropertyMsg() {
        StringBuilder sb = new StringBuilder("Hello");
        sb.append("\n")
        .append("\t").append("HOME: ").append(System.getProperty("user.dir")).append("\n")
        .append("\t").append("USER: ").append(System.getProperty("user")).append("\n")
        .append("\t").append("TIME: ").append(LocalDateTime.now()).append("\n");

        return sb.toString();
    }

    public String runTest(String msg) {
        System.err.println(msg);
        return msg;
    }
}

Repository interface:

package hu.citec.hazi.hazi.repo;

public interface Repository {

String getPropertyMsg();

String runTest(String msg);
}

Service class:

package hu.citec.hazi.hazi.service;

import hu.citec.hazi.hazi.repo.Repository;

@org.springframework.stereotype.Service
public class Service {

private Repository repo;


public String print() {
    return repo.getPropertyMsg();
}

public String test(String msg) {
    return  repo.runTest(msg);
}
}

MainConfig class:

package hu.citec.hazi.hazi.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.PropertySource;

import hu.citec.hazi.hazi.entity.User;


@ComponentScan("hu.citec.hazi.hazi")
@PropertySource({ "classpath:application.properties", "classpath:application-${spring.active.profiles}.properties" })
public class MainConfig {

@Bean
public User defaultUser(@Value("${project.profile.name}") String name) {

    System.out.println("Properties value: "+ name );
    return new User(name);
}



}

Main class:

package hu.citec.hazi.hazi;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Component;

import hu.citec.hazi.hazi.config.MainConfig;
import hu.citec.hazi.hazi.service.Service;

@Component
public class Main {

    @Autowired
    private Service service;

    public static void main(String[] args) {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(MainConfig.class);
        Main main = ctx.getBean(Main.class);
        main.run();
    }

    public void run() {
        System.out.println("App is running...");

        System.out.println("ÜDV! \n\t" + service.print() + "\n " +service.test("Sikeres teszt"));
    }
}

Maybe I have made some mistakes. Sorry about that.

I also got to write more details to make this qquestion pass.

Giancarlo Romeo
  • 663
  • 1
  • 9
  • 24
  • you are not injecting the Repository class in your service class. You need to use either Autowired or Resource class to inject it. – akshaya pandey Apr 20 '20 at 15:14
  • Well, the error indicates that in line 12 of your service class, you have a `NPE` - looking at your service class, you can see that you are calling a method (`.getPropertyMsg();`) on an object (`repo`) which may be null (i.e. not instantiated yet) – blurfus Apr 20 '20 at 15:15
  • But isnt the repo using an interface where I have written the methods behaviour? –  Apr 20 '20 at 15:28
  • service is not aware of what repo is (it's an interface) or what the implementation of the interface looks like. If you look at the service class there is nothing in it to indicate what to do with that variable. You need to either manually create the instance or you could try autowiring it – blurfus Apr 20 '20 at 15:59
  • @akshayapandey Thank you!! –  Apr 20 '20 at 15:59
  • 1
    @blurfus thank you too for explaination!! –  Apr 20 '20 at 16:00

0 Answers0