Hi there, when calling a method from another class I get the following error message:

Exception in thread "main" java.lang.NullPointerException
at TextAnalyser.main(TextAnalyser.java:23)

My two classes:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.String.*;
import java.util.*;

public class TextAnalyser {

    static Tokeniser aTokeniser;

    public TextAnalyser(Tokeniser aTokeniser) {

public static void main( String args[] )
    int sentence;
    Scanner scanner = new Scanner( System.in );
    System.out.println( "Type 1 and press Enter" );
    sentence = Integer.parseInt(scanner.nextLine());
    if(sentence == 1) { 


import java.util.ArrayList;
import java.util.Iterator;
import java.util.StringTokenizer;

public class Tokeniser {

    StringTokenizer aToken;
    ArrayList words = new ArrayList();

    public Tokeniser(StringTokenizer aToken) {
        aToken = new StringTokenizer("You are Tokenizing a String");
        public void add() {
            System.out.println("The total no. of tokens generated :  " + aToken.countTokens());

Line 23 is : aTokeniser.add();
so it obviously doesn't like what i'm pointing too, I'm thinking maybe that i need to define a return type? im not sure if ive used the constructors correctly.

Recommended Answers

All 2 Replies

no, you didn't instantiate aTokeniser, so your aTokeniser variable is still null.

Would i need to add something like this in my TextAnalyser class then?

aTokeniser = new Tokeniser( );

Also i'm not sure if i've done the Tokeniser Constructor correctly

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.