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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:56:46+00:00 2026-06-05T13:56:46+00:00

I am playing around with Tomcat authentication using Mysql. To facilitate this I’m trying

  • 0

I am playing around with Tomcat authentication using Mysql. To facilitate this I’m trying to model the Tomcat user data stored in the database. So I built a TomcatUser class.

I’ve stored the user name and password as Strings to just make life easy during testing. Security will be implemented later. And the user’s Tomcat roles are stored in an ArrayList so I can easily search and manage the user’s roles. And I have a few constructors for different scenarios.

One of these is using a String VARARG to set however many Tomcat roles the user may have. I know that the parameters in a VARARG are stored in an array. In this case a String Array. I use the Collections.addAll() method to add the contents of the VARARG array to the object’s ArrayList.

In the test class I’m getting a NullPointerException from instantiating an Iterator from the object’s , probably for some silly mistake on my part. But then I’m new. Below is my code, for both the model class and the test. Can you tell me what I’m doing wrong? And any tips or advice in general would be appreciated.

The model.

/*
 * Class modeling user data for Tomcat.
 */

package tomcat;

import java.util.ArrayList;
import java.util.Collections;

public class TomcatUser
{
    String tomcatUserName; //Field to store Tomcat user's name.
    String tomcatUserPassword; //Field to store Tomcat user's password.

    /*
     * Field to store the Tomcat user's roles as an ArrayList. This allows for easy element checking.
     */
    ArrayList<String> tomcatUserRoles;

    /*
     * Constructor intended for creating a roleless user. Not much use, but who knows.
     */
    public TomcatUser(String tomcatUserName, String tomcatUserPassword)
    {
            super();
            this.setTomcatUserName(tomcatUserName);
            this.setTomcatUserPassword(tomcatUserPassword);
        }

        /*
         * Constructors for instantiating for general use.
         * Three constructors are provided to provide options for instantiation.
         */
        public TomcatUser(String tomcatUserName, ArrayList<String> tomcatUserRoles)
        {
            super();
            this.setTomcatUserName(tomcatUserName);
        this.setTomcatUserRoles(tomcatUserRoles);
    }

    public TomcatUser(String tomcatUserName, String... tomcatUserRoles)
    {
        super();
        this.setTomcatUserName(tomcatUserName);
    }
    /*
     * 
     */

    /*
     * Constructors intended for adding a new user to the database.
     * Three constructors are provided to provide options for instantiation.
     */
    public TomcatUser(String tomcatUserName, String tomcatUserPassword, ArrayList<String> tomcatUserRoles)
    {
        this(tomcatUserName, tomcatUserPassword);
        this.setTomcatUserRoles(tomcatUserRoles);
    }
    /*
     * 
     */

    /*
     * Standard setters and getters.
     */
    /**
     * @return the tomcatUserName
     */
    public String getTomcatUserName() {
        return tomcatUserName;
    }

    /**
     * @param tomcatUserName the tomcatUserName to set
     */
    public void setTomcatUserName(String tomcatUserName) {
        this.tomcatUserName = tomcatUserName;
    }

    /**
     * @return the tomcatUserPassword
     */
    public String getTomcatUserPassword() {
        return tomcatUserPassword;
    }

    /**
     * @param tomcatUserPassword the tomcatUserPassword to set
     */
    public void setTomcatUserPassword(String tomcatUserPassword) {
        this.tomcatUserPassword = tomcatUserPassword;
    }

    /**
     * @return the tomcatUserRoles
     */
    public ArrayList<String> getTomcatUserRoles() {
        return tomcatUserRoles;
    }

    /**
     * @param tomcatUserRoles the tomcatUserRoles to set
     */
    public void setTomcatUserRoles(ArrayList<String> tomcatUserRoles) {
        this.tomcatUserRoles = tomcatUserRoles;
    }
    /*
     * 
     */

    /*
     * Setter to handle input options from matching constructors, and to provide options for setting tomcatUserRoles field.
     */

    public void setTomcatUserRoles(String[] newTomcatUserRoles)
    {
        Collections.addAll(this.tomcatUserRoles, newTomcatUserRoles);
    }
    /*
     * 
     */

    /*
     * Method to add a role to the object.
     */
    public void addTomcatUserRole (String newTomcatUserRole)
    {
        this.tomcatUserRoles.add(newTomcatUserRole);
    }

    public Boolean hasTomcatRole (String checkTomcatRole)
    {
        return this.tomcatUserRoles.contains(checkTomcatRole);
    }
}

And the test class.

package tomcat;

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

public class TomcatUserTest
{

    static ArrayList<String> testTomcatRoles = new ArrayList<String>();
    /**
     * @param args
     */
    public static void main(String[] args)
    {
        for (Integer iterator = 1; iterator <4 ; iterator++)
        {
            testTomcatRoles.add("testTomcatRole" + iterator.toString());
        }

        TomcatUser testTomcatUser01 = new TomcatUser("testUser01", "testPassword01");

        System.out.print("testTomcatUser01 contents: ");
        System.out.print(testTomcatUser01.getTomcatUserName() + " " + testTomcatUser01.getTomcatUserPassword());
        System.out.println();

        TomcatUser testTomcatUser02 = new TomcatUser("testUser02", testTomcatRoles);

        System.out.print("testTomcatUser02 contents: ");
        System.out.print(testTomcatUser02.getTomcatUserName() + " ");

        Iterator<String> testTomcatUser02Iterator = testTomcatUser02.getTomcatUserRoles().iterator();

        while (testTomcatUser02Iterator.hasNext())
        {
            System.out.print(testTomcatUser02Iterator.next());

            if (testTomcatUser02Iterator.hasNext())
            {
                System.out.print(" ");
            }
        }

        System.out.println();

        TomcatUser testTomcatUser03 = new TomcatUser("testTomcatUser03", "testPassword03", testTomcatRoles);

        System.out.print("testTomcatUser03 contents: ");
        System.out.print(testTomcatUser03.getTomcatUserName() + " " + testTomcatUser03.getTomcatUserPassword() + " ");

        Iterator<String> testTomcatUser03Iterator = testTomcatUser03.getTomcatUserRoles().iterator();

        while (testTomcatUser03Iterator.hasNext())
        {
            System.out.print(testTomcatUser03Iterator.next());

            if (testTomcatUser03Iterator.hasNext())
            {
                System.out.print(" ");
            }
        }

        System.out.println();

        TomcatUser testTomcatUser04 = new TomcatUser("testTomcatUser04", "testRole1", "testRole2", "testRole3");

        System.out.print("testTomcatUser04 contents: ");
        System.out.print(testTomcatUser04.getTomcatUserName() + " ");

        Iterator<String> testTomcatUser04Iterator = testTomcatUser04.getTomcatUserRoles().iterator();

        while (testTomcatUser04Iterator.hasNext())
        {
            System.out.print(testTomcatUser04Iterator.next());

            if (testTomcatUser04Iterator.hasNext())
            {
                System.out.print(" ");
            }
        }
    }

}
  • 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-05T13:56:48+00:00Added an answer on June 5, 2026 at 1:56 pm

    This constructor:

    public TomcatUser(String tomcatUserName, String... tomcatUserRoles)
    

    never assigns the field tomcatUserRoles.

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

Sidebar

Related Questions

After playing around with haskell a bit I stumbled over this function: Prelude Data.Maclaurin>
Am playing around with the standard Wordpress search and am using this code in
Playing around with Flask and just wanted to print out some data as JSON
While playing around with the emulator, I noticed that when trying to view a
Im playing around with the Tablesorter plug-in for jQuery and was trying to get
Playing around with generating text randomly with each page refresh using php. Is there
Just playing around with java trying to learn it etc. Here is my code
I've been playing around with Simple.Data and have run across something that I can't
Playing around with jQuery a bit (sorry, complete noob) I was wondering why this
in playing around with the idea of using a webservice for my project I

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.