Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullPointerException when registering a ("dynamic") command when auto save configuration is disabled #35

Closed
3 tasks done
SirSkaro opened this issue Feb 11, 2018 · 0 comments

Comments

@SirSkaro
Copy link

SirSkaro commented Feb 11, 2018

Prerequisites

  • If this is a question/suggestion, I have already considered talking about it on Discord
  • This issue specifically has something to do with Twitch4J
  • I have attempted to look for similar issues already

Description

When creating a TwitchClientBuilder and including .withAutoSaveConfiguration(false), I will get a stack trace when trying to register a command in said TwitchClient's CommandHandler. The stack trace only occurs when trying to register an instance of a Command (i.e. TestCommand()), not when registering the class (TestCommand.class).

Triggers stack trace

TestCommand command = new TestCommand();
twitchClient.getCommandHandler().registerCommand(command);

No stack trace

twitchClient.getCommandHandler().registerCommand(TestCommand.class);

Steps to Reproduce

  1. Create a TwitchClient instance via the TwitchClientBuilder (making sure to use .withAutoSaveConfiguration(false) )
TwitchClient twitchClient = TwitchClientBuilder.init()
	.withClientId("")
	.withClientSecret("")
	.withAutoSaveConfiguration(false)
	.withCredential("") 
	.connect();
  1. Create an instance of a class that extends the Command class
TestCommand command = new TestCommand();
  1. Register the command in the TwitchClient instance's CommandHandler
twitchClient.getCommandHandler().registerCommand(command);

Expected behavior:
The Command should register with not stack trace.

Actual behavior:
The Command is registered to the CommandHandler and I am able to use it in Twitch chat, however I get a stack trace during the registration.

Stacktrace (if applicable):
https://hastebin.com/ehiwuxelar.xml

Version affected:
0.10.1

Additional Information

I believe this is a bug because of the logic of the method where the exception occurs.

public void saveDynamicCommands() {
		try {
			// Get all Dynamic Commands
			List<Command> commandList = getCommandMap().values().stream().filter(e -> e instanceof DynamicCommand).collect(Collectors.toList());

			// Save List to File
			ObjectMapper mapper = new ObjectMapper();
			mapper.writeValue(getCommandSaveFile(), commandList);

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

The exception occurs at the line mapper.writeValue(getCommandSaveFile(), commandList); (line 283). A NullPointerException is thrown because there is no command save file because saving configurations was disabled when creating the TwitchClient instance. If users are given the option to disable saving configurations, then either the exception should be handled quietly or there should be logic to check the user's preferences before attempting to write to the file and printing the error.

I tried to test this theory by enabling saving configurations (.withAutoSaveConfiguration(true)) and repeating the process. As expected, no stack trace occurred, supporting my belief that this is a bug.

It's entirely possible that I am using this library in an unintended manner, however if I understand what is happening correctly then I think this is an issue.

@stachu540 stachu540 added this to In Progress in 1.x May 20, 2018
stachu540 pushed a commit that referenced this issue Jun 3, 2018
@stachu540 stachu540 moved this from In Progress to Done in 1.x Jun 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
1.x
  
Done
Development

No branches or pull requests

2 participants