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

  • Home
  • SEARCH
  • 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 8890235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:27:17+00:00 2026-06-14T22:27:17+00:00

I get some errors when I run my Java code. It compiles fine, but

  • 0

I get some errors when I run my Java code. It compiles fine, but I get runtime errors with exceptions. This is the code:

import java.io.*;
class display {

private int charNumber;
private char[] currentArray;

public display() {

    charNumber = 0;

    }

public void dispText(String text, long speed, long wait) {
    while(currentArray[charNumber] != '~') {
        currentArray = text.toCharArray();
        System.out.print(currentArray[charNumber]);
        try {
            Thread.sleep(speed);
        } catch (NullPointerException e) {
            System.out.println("Error in the Thread process:\n" + e);
        } catch (InterruptedException e) {
            System.out.println("Error in the Thread process:\n" + e);
        }
        charNumber++;
        }
    charNumber = 0;
    try {
        Thread.sleep(wait);
    } catch (NullPointerException e) {
        System.out.println("Error in the Thread process:\n" + e);
    } catch (InterruptedException e) {
        System.out.println("Error in the Thread process:\n" + e);
    }
}

public void resetCharNumber() {
    charNumber = 0;
    }
}



class Main {
public static void main (String[] args) throws Exception {
    //Make sure to include a '~' at the end of every String.    
    String start = "Hey, is this thing on?~";
    String hello = "Hello, World!~";
    display d = new display();
    d.dispText(start, 200, 2000);
    d.dispText(hello, 200, 2000);
    System.out.println("\nDone!");
}
}

The void dispText takes a string to display text with System.out.print, the long speed to determine how much time passes every time a character is displayed (like a typewriter) and the long wait, to determine how much time passes before the next process is executed. dispText takes the String text, converts it to a char array with text.toCharArray();, and then goes into a while loop where it displays one character per run, then waits the time designated by speed, and then moves on to the next character. It does so until it reaches the last character (‘~’) which is included as the last character in the Strings that are fed to text. Then, it moves on to the next line. Then in main, an istance of the display class is created, named ‘d’, and d executes dispText twice.

This is the runtime error I get when I run it:

Runtime error: Exception in thread “main” java.lang.NullPointerException
at display.dispText(Main.java:14)
at Main.main(Main.java:48)

  • 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-14T22:27:19+00:00Added an answer on June 14, 2026 at 10:27 pm

    You declared your array like: –

    private char[] currentArray;
    

    But you never initialized it. You should initialize it in constructor like: –

    currentArray = new char[size];
    

    OR, as noted in the comment, you are rather initializing the array, but at wrong location.

    You have this code in your while loop: –

    while(currentArray[charNumber] != '~') {
            currentArray = text.toCharArray();
    

    just move the first statement outside the while loop: –

    currentArray = text.toCharArray();  // Move this outside the while
    while(currentArray[charNumber] != '~') {
    

    And then you won’t need to initialize the array in constructor.


    As a side note, please follow Java Naming Conventions. Class name should start with Uppercase letters and should follow CamelCasing thereafter.

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

Sidebar

Related Questions

I want to compile the Rigi source code but I get some error while
I have some Java code: public class Car { private int yearModel; private String
I'm running some 3d example code from oracle in Eclipse, but I get many
I have some java code that looks similar to this: private void startServer() throws
I have seen some other questions about this but the errors were related to
I have some logic (java.lang.ArrayIndexOutOfBoundsException) error in this part of my code for some
I am trying to get some errors returned in JSON format. So, I made
What's wrong in here, I always get some nasty errors even if the same
I have been trying to get my head around some errors I get when
I am trying to parse some XML, however, I get the error above this

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.