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

[null-in-exception.msg] stack trace #18108

Merged
merged 1 commit into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions core-java-modules/core-java-exceptions-5/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## Core Java Exceptions

This module contains articles about core java exceptions

### Relevant articles:
15 changes: 15 additions & 0 deletions core-java-modules/core-java-exceptions-5/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-exceptions-5</artifactId>
<packaging>jar</packaging>
<name>core-java-exceptions-5</name>

<parent>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package com.baeldung.exception.nullmessage;

import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class Team {

private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

class Player {

private static Logger LOG = LoggerFactory.getLogger(Player.class);
private String name;
private Team team;

public Player(String name) {
this.name = name;
}

public void output() {
try {
if (team != null) {
LOG.info("Player: {}, Team: {}", name.toUpperCase(), team.getName().toUpperCase());
} else {
throw new IllegalArgumentException("Play's team is null");
}
} catch (Exception e) {
LOG.error("Error occurred." + e.getMessage());
}
}

public void outputWithStackTrace() {
try {
if (team != null) {
LOG.info("Player: {}, Team: {}", name.toUpperCase(), team.getName().toUpperCase());
} else {
throw new IllegalArgumentException("Play's team is null");
}
} catch (Exception e) {
e.printStackTrace();
}
}

public void outputWithStackTraceLog() {
try {
if (team != null) {
LOG.info("Player: {}, Team: {}", name.toUpperCase(), team.getName().toUpperCase());
} else {
throw new IllegalArgumentException("Play's team is null");
}
} catch (Exception e) {
LOG.error("Error occurred.", e);
}
}


public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Team getTeam() {
return team;
}

public void setTeam(Team team) {
this.team = team;
}

}

public class ExceptionGetMessageNullUnitTest {

@Test
void whenCallingPlayerOutput_thenGetExceptionWithNullMessage() {
Player kai = new Player("Kai");
kai.setTeam(new Team());

kai.output();
}

@Test
void whenCallingPlayerOutputWithStackTrace_thenGetStackTrace() {
Player kai = new Player("Kai");
kai.setTeam(new Team());

kai.outputWithStackTrace();
}

@Test
void whenCallingPlayerOutputWithStackTraceLog_thenGetStackTrace() {
Player kai = new Player("Kai");
kai.setTeam(new Team());

kai.outputWithStackTraceLog();
}
}
1 change: 1 addition & 0 deletions core-java-modules/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
<module>core-java-exceptions-2</module>
<module>core-java-exceptions-3</module>
<!-- <module>core-java-exceptions-4</module> --> <!-- failing after upgrade to jdk21 -->
<module>core-java-exceptions-5</module>
<module>core-java-function</module>
<module>core-java-functional</module>
<module>core-java-hex</module>
Expand Down