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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:17:03+00:00 2026-05-26T20:17:03+00:00

I was trying to do a simple implementation of the command patter in java.

  • 0

I was trying to do a simple implementation of the command patter in java. However, I am getting the following error:

java.lang.ClassNotFoundException: AddCommand
Exception in thread "main" java.lang.NullPointerException
at com.programming.sample.TransactionCommand.execute(TestTransactionCommand.java:64)
at com.programming.sample.CommandManager.runCommands(TestTransactionCommand.java:33)
at com.programming.sample.TestTransactionCommand.main(TestTransactionCommand.java:124)

Code:

            package com.programming.sample;

            //TestTransactionCommand.java
            import java.util.*;
            final class CommandReceiver {
              private int[] c;
              private CommandArgument a;
                 private CommandReceiver(){
                   c = new int[2];
                 }
                 private static CommandReceiver cr = new CommandReceiver();
                 public static CommandReceiver getHandle() {
                 return cr;
                 }
                 public void setCommandArgument(CommandArgument a) {
                 this.a = a;
                 }
                 public void methAdd() {
                 c = a.getArguments();
                     System.out.println("The result is " + (c[0]+c[1]));
                 }
                 public void methSubtract() {
                 c = a.getArguments();
                     System.out.println("The result is " + (c[0]-c[1]));
                 }
            }
             class CommandManager {
               private Command myCommand;
               public CommandManager(Command  myCommand) {
                 this.myCommand  =  myCommand ;    
               }
               public void runCommands( ) {
                        myCommand.execute();     
               }
             }
            class TransactionCommand implements Command {
              private CommandReceiver commandreceiver;
              private Vector commandnamelist,commandargumentlist; 
              private String commandname;
              private CommandArgument commandargument;
              private Command command;
              public TransactionCommand () {
                this(null,null);
              }
              public TransactionCommand ( Vector  commandnamelist, Vector
            commandargumentlist){
                this.commandnamelist = commandnamelist;
                this.commandargumentlist = commandargumentlist;
                commandreceiver =  CommandReceiver.getHandle();  
              }
              public void execute( ) {
                for (int i = 0; i < commandnamelist.size(); i++) {
                  commandname = (String)(commandnamelist.get(i));
                  commandargument = (CommandArgument)((commandargumentlist.get(i)));
                  commandreceiver.setCommandArgument(commandargument);
                  String classname = commandname + "Command";
                     try {
                       Class cls = Class.forName(classname);
                       command = (Command) cls.newInstance();
                     }
                     catch (Throwable e) {   
                              System.err.println(e);
                     }
                  command.execute();
                } 
              }
            }
             class AddCommand extends TransactionCommand {
               private CommandReceiver cr;
               public AddCommand () {
                  cr = CommandReceiver.getHandle();  
               }  
               public void execute( ) {  
                 cr.methAdd();  
               }   
             }
             class SubtractCommand extends TransactionCommand {
               private CommandReceiver cr;
               public SubtractCommand () {
                  cr = CommandReceiver.getHandle();  
               }
               public void execute( ) {
                 cr.methSubtract();
               }   
             }
             class CommandArgument {
               private int[] args;
               CommandArgument() {
                 args = new int[2];
               }
               public int[] getArguments() {
                return args;
               }
               public void setArgument(int i1, int i2) {
                     args[0] = i1; args[1] = i2;
               }
             }
             public class TestTransactionCommand {
               private  Vector clist,alist; 
               public TestTransactionCommand() {
                 clist = new Vector(); 
                   alist = new Vector();
               }
               public void clearBuffer(Vector c, Vector a) {
                 clist.removeAll(c);
                   alist.removeAll(a); 
               }
               public Vector getClist() {
                 return clist;
               }
               public Vector getAlist() {
                 return alist;
               }
                public static void main(String[] args) {

                 CommandArgument ca,ca2;
                 TestTransactionCommand t = new TestTransactionCommand();
                 ca = new CommandArgument();
                 ca.setArgument(2,8);
                 Vector myclist = t.getClist();
                 Vector myalist = t.getAlist();
                 myclist.addElement("Add"); 
                 myalist.addElement(ca);
                 TransactionCommand tc = new TransactionCommand(myclist,myalist);
                 CommandManager cm = new CommandManager(tc);       
                 cm.runCommands();

                 t.clearBuffer(myclist,myalist);
                 ca2 = new CommandArgument();
                 ca2.setArgument(5,7);
                 myclist = t.getClist();
                 myalist = t.getAlist();
                 myclist.addElement("Subtract"); 
                 myalist.addElement(ca2);
                 myclist.addElement("Add"); 
                 myalist.addElement(ca2);
                 TransactionCommand tc2 = new TransactionCommand(myclist,myalist);        
                 CommandManager cm2 = new CommandManager(tc2);       
                 cm2.runCommands();
               }
             } 
  • 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-05-26T20:17:04+00:00Added an answer on May 26, 2026 at 8:17 pm

    When you create the classname, the string contains only AddCommand. The class’ actual name is com.programming.sample.AddCommand.

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

Sidebar

Related Questions

I am trying to do a very simple command line library for interactive Java
I'm trying to create and compile a simple Objective-C program from the command line.
I am trying to make a Java port of a simple feed-forward neural network.
I am trying out a simple implementation of a URL shortener such as tinyurl.com
I'm trying to write a simple implementation of a patience sort, using Scala. I've
I'm using a simple queue implementation and I'm trying to make a simple program
I'm using the JMockit framework and I'm trying to test my simple EventBus implementation
I am trying to make AspNetComet.zip work on IIS7 (a simple comet chat implementation)
Trying to find a simple to use safe_bool idiom/implementation, I've ended up with my
I am trying to use a very simple implementation of the scrollTo plugin: http://jsfiddle.net/TPQyY/

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.