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 8747787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:24:54+00:00 2026-06-13T12:24:54+00:00

I have a maven project with the following structure: spring-di-test +–spring-di-test-core | com.example.core.DITestMain +–spring-di-test-store

  • 0

I have a maven project with the following structure:

spring-di-test
 +--spring-di-test-core
 |     com.example.core.DITestMain
 +--spring-di-test-store
       com.example.store.DITest
       com.example.store.RandomStringService

where spring-di-test is the root project and the two below are modules.

My classes look like this:

DITestMain located in spring-di-test-core

public class DITestMain {
    public static void main(String[] args) {
        new DITest().run();
    }
}

applicationContext.xml located in spring-di-test-core’s resources folder

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:spring-configured/>
    <context:component-scan base-package="com.example.*" annotation-config="true"/>

</beans>

DITest located in spring-di-test-store

@Configurable(preConstruction = true)
@Controller
public class DITest {   
    @Autowired(required=true)
    private RandomStringService randomStringService;

    public void run() {
        System.out.println(randomStringService.getRandomString());
    }
}

RandomStringService located in spring-di-test-store

@Service("randomStringService")
public class RandomStringService {

    private final Random random;

    public RandomStringService() {
        random = new Random();
    }

    public String getRandomString() {
        StringBuilder sb = new StringBuilder();
        int length = random.nextInt(20);
        for (int i = 0; i < length + 1; i++) {
            sb.append(new Character((char) ('a' + random.nextInt(20))));
        }
        return sb.toString();
    }
}

applicationContext.xml located in spring-di-test-store

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:spring-configured/>
    <context:component-scan base-package="com.example.*" annotation-config="true"/>

</beans>

When I run DITestMain, I get a NullPointerException for randomStringService. What went wrong?

  • 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-13T12:24:55+00:00Added an answer on June 13, 2026 at 12:24 pm

    1: You’re not creating the Spring context in your main method

    2: Have you done all the required configuration to enable load-time weaving (so that your @Configurable bean will work)?

    Take a look at http://static.springsource.org/spring/docs/3.0.x/reference/aop.html#aop-using-aspectj and read the entire chapter closely.

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

Sidebar

Related Questions

I have some pom files in my project with the following structure <project xmlns=http://maven.apache.org/POM/4.0.0
I have the following Maven project estruture: Example -Persistence -GWTApp The persistence has the
I have integrated in Hudson a maven 3 project calling following sequence: clean tomcat:redeploy
I have Maven project with Hibernate and Spring framework. I want Hibernate to create
I have a maven project that was generated by Spring Roo. Now I am
I've noticed that a lot of projects have the following structure: Project-A bin lib
I have following situation: I have a Maven war project in NetBeans 6.7 I
Currently, I have the following structure for my project Foo : foo-parent +- pom.xml
I have a java/maven project that uses tango icons, so I'm using the following
I have a problem with following tutorial: http://www.mkyong.com/jsf2/jsf-2-internationalization-example/ In faces-context file you have to

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.