Hallo
wir machen derzeit in der FH einen Mediaplayer und ich als Anfänger habe echt Probleme damit.
Kann sein dass der Code teilweise wohl ziemlich komisch ist und sinnlos aber ich sitze schon unzählige Stunden daran und kann einfach nicht alle Fehler beseitigen.
Der Test ist vorgegeben und muss so bestanden werden. Leider wird mir nicht angzeigt in welcher Zeile im Code der Fehler steckt.
Diese Fehler werden mir ausgegeben:
junit.framework.AssertionFailedError: Fehler fuer pathname::java.lang.NullPointerException
at junit.framework.Assert.fail(Assert.java:47)
at AudioFileTest.testCtor(AudioFileTest.java:211)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:164)
at junit.framework.TestCase.runBare(TestCase.java:130)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:120)
at junit.framework.TestSuite.runTest(TestSuite.java:230)
at junit.framework.TestSuite.run(TestSuite.java:225)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
junit.framework.AssertionFailedError: Fehler fuer pathname::java.lang.NullPointerException
at junit.framework.Assert.fail(Assert.java:47)
at AudioFileTest.testSettersAndGetters(AudioFileTest.java:183)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:164)
at junit.framework.TestCase.runBare(TestCase.java:130)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:120)
at junit.framework.TestSuite.runTest(TestSuite.java:230)
at junit.framework.TestSuite.run(TestSuite.java:225)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Ich hoffe mir kann jemand helfen.
Hier der Code:
Hier der Test:
wir machen derzeit in der FH einen Mediaplayer und ich als Anfänger habe echt Probleme damit.
Kann sein dass der Code teilweise wohl ziemlich komisch ist und sinnlos aber ich sitze schon unzählige Stunden daran und kann einfach nicht alle Fehler beseitigen.
Der Test ist vorgegeben und muss so bestanden werden. Leider wird mir nicht angzeigt in welcher Zeile im Code der Fehler steckt.
Diese Fehler werden mir ausgegeben:
junit.framework.AssertionFailedError: Fehler fuer pathname::java.lang.NullPointerException
at junit.framework.Assert.fail(Assert.java:47)
at AudioFileTest.testCtor(AudioFileTest.java:211)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:164)
at junit.framework.TestCase.runBare(TestCase.java:130)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:120)
at junit.framework.TestSuite.runTest(TestSuite.java:230)
at junit.framework.TestSuite.run(TestSuite.java:225)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
junit.framework.AssertionFailedError: Fehler fuer pathname::java.lang.NullPointerException
at junit.framework.Assert.fail(Assert.java:47)
at AudioFileTest.testSettersAndGetters(AudioFileTest.java:183)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:164)
at junit.framework.TestCase.runBare(TestCase.java:130)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:120)
at junit.framework.TestSuite.runTest(TestSuite.java:230)
at junit.framework.TestSuite.run(TestSuite.java:225)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Ich hoffe mir kann jemand helfen.
Hier der Code:
Java:
public class AudioFile {
private String file, path1 = "", path2, filename1, title, author;
private int j;
private char sepc = java.io.File.separatorChar;
private String sep = "" + sepc;
private boolean isWindows() {
return System.getProperty("os.name").toLowerCase().indexOf("win") >= 0;
}
public AudioFile() {
}
public AudioFile(String pathname) {
parsePathname(pathname);
parseFilename(getFilename());
}
public String toString() {
if (getAuthor().isEmpty()) {
return getTitle();
} else {
return getAuthor() + " - " + getTitle();
}
}
public void parsePathname(String pathname) {
pathname = pathname.replace('/', sepc).replace('\\', sepc);
if (pathname.isEmpty()) {
path2 = "";
} else {
for (j = 0; j < pathname.length();) {
if (j < pathname.length() - 1) {
if (pathname.charAt(j) == sepc
&& pathname.charAt(j + 1) == sepc) {
j++;
} else if (pathname.charAt(j) == sepc
&& pathname.charAt(j + 1) != sepc) {
path1 += pathname.charAt(j);
j++;
} else {
path1 += pathname.charAt(j);
j++;
}
} else {
path1 += pathname.charAt(j);
j++;
}
}
if (path1.contains(":")) {
if (isWindows()) {
path2 = path1.charAt(0) + ":" + sep + path1.substring(3);
} else {
path2 = sep + path1.charAt(0) + sep + path1.substring(3);
}
}
else if (path1.charAt(0) == sepc) {
path2 = sep + path1.substring(1);
} else
path2 = path1;
if ((path2.charAt(path2.length() - 1)) == java.io.File.separatorChar) {
file = "";
} else if (!path2.contains(sep)) {
file = path2;
}
else {
file = path2.substring(path2
.lastIndexOf(java.io.File.separatorChar) + 1);
}
}
}
public String getPathname() {
return path2;
}
public String getFilename() {
return file;
}
public void parseFilename(String filename) {
if (!filename.contains("-") && filename.contains(".")) {
author = "";
title = filename.substring(0, filename.lastIndexOf('.')).trim();
}
else if (filename.contains("-") && filename.contains(".")) {
filename1 = filename.substring(0, filename.lastIndexOf('.'));
author = filename1.substring(0, filename1.indexOf(" - ")).trim();
title = filename1.substring(filename1.indexOf(" - ") + 3).trim();
} else if (filename.contains("-") && !filename.contains(".")) {
if (filename.substring(0, filename.indexOf("-")).isEmpty()) {
author = "";
title = filename;
} else {
author = filename.substring(0, filename.indexOf("-")).trim();
title = filename.substring(filename.indexOf("-") + 1).trim();
}
}
}
public String getAuthor() {
return author;
}
public String getTitle() {
return title;
}
}
Hier der Test:
Java:
// We still use the Junit 3 framework since the APA server is not yet migrated to JUnit 4
// This is the version that runs on the PCs.
//
// It provides the facility to emulate a different OS and enables the developer
// to check the behavior of the test code on platforms different from the
// developers platform.
// The facility is convenient to check the normalization of pathnames with respect to
// '/' and '\'
//
// The behavior is defined within the method setUp() below.
//
// Note: at the APA server we are running the linux emulation!
//
import junit.framework.TestCase;
public class AudioFileTest extends TestCase {
private static char sep;
private static String root = "/";
private static String pathNames[];
private static String expectedPathNames[];
private static String expectedFileNames[];
private static String authors[];
private static String titles[];
private static String toStrings[];
public void setUp() {
try {
// Activate exactly one the following lines
// in order to define the emulation you like
// Usually, the first line (no emulation) is active
//
EmulateOtherOs.reset(); // no emulation
// EmulateOtherOs.emulateWindows();
// EmulateOtherOs.emulateLinux();
// EmulateOtherOs.emulateMac();
} catch (Exception e) {
e.printStackTrace();
}
sep = java.io.File.separatorChar;
String osname = System.getProperty("os.name");
if (osname.toLowerCase().indexOf("win") >= 0)
root = "C:" + sep;
// This array contains the arguments we feed to method parsePathname()
pathNames = new String[] {
root + "home" + sep + "meier" + sep + "Musik" + sep
+ "Falco - Rock Me Amadeus.mp3",
root + "home" + sep + "db-admin" + sep
+ "Frankie Goes To Hollywood - The Power Of Love.ogg",
root + "tmp" + sep + "Deep Purple - Smoke On The Water.wav",
root + "my-tmp" + sep + "file.mp3",
"Falco - Rock Me Amadeus.mp3",
"file.mp3",
".." + sep + "music" + sep + "audiofile.au",
" A.U.T.O.R - T.I.T.E.L .EXTENSION",
"Hans-Georg Sonstwas - Blue-eyed boy-friend.mp3",
// Some more ugly test cases.
// Note that arbitrary combinations of / and \ are provided.
// Consecutive occurrences of these are to be squeezed and
// replaced by a single separator that corresponds to
// the platform running the application (use
// java.io.File.separatorChar).
// Further note that spaces and tabs (white space) are not
// altered by this normalization.
"", " ", "//your-tmp/part1//file.mp3/",
"../your-tmp/..//part1//file.mp3/", "\\file.mp3",
"\\part1\\\\file.mp3\\", "\\part1///file.mp3",
"/MP3-Archiv/.nox", "/MP3-Archiv/Falco - Rock me Amadeus.",
"-", " - " };
// Array of the results expected from method getPathname()
// We expect normalization with respect to consecutive occurrences of /
// and \
// and replacement by a single java.io.File.separatorChar
// Spaces and tabs (white space) are not altered.
expectedPathNames = new String[] {
root + "home" + sep + "meier" + sep + "Musik" + sep
+ "Falco - Rock Me Amadeus.mp3",
root + "home" + sep + "db-admin" + sep
+ "Frankie Goes To Hollywood - The Power Of Love.ogg",
root + "tmp" + sep + "Deep Purple - Smoke On The Water.wav",
root + "my-tmp" + sep + "file.mp3",
"Falco - Rock Me Amadeus.mp3",
"file.mp3",
".." + sep + "music" + sep + "audiofile.au",
" A.U.T.O.R - T.I.T.E.L .EXTENSION",
"Hans-Georg Sonstwas - Blue-eyed boy-friend.mp3",
// Some more ugly test cases
"",
" ",
sep + "your-tmp" + sep + "part1" + sep + "file.mp3" + sep,
".." + sep + "your-tmp" + sep + ".." + sep + "part1" + sep
+ "file.mp3" + sep, sep + "file.mp3",
sep + "part1" + sep + "file.mp3" + sep,
sep + "part1" + sep + "file.mp3",
sep + "MP3-Archiv" + sep + ".nox",
sep + "MP3-Archiv" + sep + "Falco - Rock me Amadeus.", "-",
" - " };
// Array of the results expected from method getFilename()
// Spaces and tabs (white space) are not altered.
expectedFileNames = new String[] { "Falco - Rock Me Amadeus.mp3",
"Frankie Goes To Hollywood - The Power Of Love.ogg",
"Deep Purple - Smoke On The Water.wav", "file.mp3",
"Falco - Rock Me Amadeus.mp3", "file.mp3", "audiofile.au",
" A.U.T.O.R - T.I.T.E.L .EXTENSION",
"Hans-Georg Sonstwas - Blue-eyed boy-friend.mp3",
// Some more ugly test cases
"", " ", "", "", "file.mp3", "", "file.mp3", ".nox",
"Falco - Rock me Amadeus.", "-", " - " };
// Array of the results expected from method getAuthor()
// Leading and trailing spaces and tabs (white space) are trimmed.
authors = new String[] { "Falco", "Frankie Goes To Hollywood",
"Deep Purple", "", "Falco", "", "", "A.U.T.O.R",
"Hans-Georg Sonstwas",
// Some more ugly test cases
"", "", "", "", "", "", "", "", "Falco", "", "" };
// Array of the results expected from method getTitle()
// Leading and trailing spaces and tabs (white space) are trimmed.
titles = new String[] { "Rock Me Amadeus", "The Power Of Love",
"Smoke On The Water", "file", "Rock Me Amadeus", "file",
"audiofile", "T.I.T.E.L", "Blue-eyed boy-friend",
// Some more ugly test cases
"", "", "", "", "file", "", "file", "", "Rock me Amadeus", "-",
"" };
// Array of the results expected from method toString()
toStrings = new String[] { "Falco - Rock Me Amadeus",
"Frankie Goes To Hollywood - The Power Of Love",
"Deep Purple - Smoke On The Water", "file",
"Falco - Rock Me Amadeus", "file", "audiofile",
"A.U.T.O.R - T.I.T.E.L",
"Hans-Georg Sonstwas - Blue-eyed boy-friend",
// Some more ugly test cases
"", "", "", "", "file", "", "file", "",
"Falco - Rock me Amadeus", "-", "" };
}
public void tearDown() {
try {
EmulateOtherOs.reset();
} catch (Exception e) {
e.printStackTrace();
}
}
public void testSettersAndGetters() {
// Use the constructor without arguments
String current = null;
try {
for (int i = 0; i < pathNames.length; i++) {
String p = pathNames[i];
current = p;
AudioFile af = new AudioFile();
af.parsePathname(p);
af.parseFilename(af.getFilename());
assertEquals("getPathname() fuer Testfall [" + i + "]: " + p
+ " nicht korrekt", expectedPathNames[i],
af.getPathname());
assertEquals("getFilename() fuer Testfall [" + i + "]: " + p
+ " nicht korrekt", expectedFileNames[i],
af.getFilename());
assertEquals("getAuthor() fuer Testfall [" + i + "]: " + p
+ " nicht korrekt", authors[i], af.getAuthor());
assertEquals("getTitle() fuer Testfall [" + i + "]: " + p
+ " nicht korrekt", titles[i], af.getTitle());
assertEquals("toString() fuer Testfall [" + i + "]: " + p
+ " nicht korrekt", toStrings[i], af.toString());
}
} catch (Exception e) {
fail("Fehler fuer pathname:" + current + ":" + e);
}
}
public void testCtor() {
// Perform the same getter and setter tests by using the ctor with one
// argument for construction
String current = null;
try {
for (int i = 0; i < pathNames.length; i++) {
String p = pathNames[i];
current = p;
AudioFile af = new AudioFile(p);
assertEquals("getPathname() fuer Testfall [" + i + "]: " + p
+ " nicht korrekt", expectedPathNames[i],
af.getPathname());
assertEquals("getFilename() fuer Testfall [" + i + "]: " + p
+ " nicht korrekt", expectedFileNames[i],
af.getFilename());
assertEquals("getAuthor() fuer Testfall [" + i + "]: " + p
+ " nicht korrekt", authors[i], af.getAuthor());
assertEquals("getTitle() fuer Testfall [" + i + "]: " + p
+ " nicht korrekt", titles[i], af.getTitle());
assertEquals("toString() fuer Testfall [" + i + "]: " + p
+ " nicht korrekt", toStrings[i], af.toString());
}
} catch (Exception e) {
fail("Fehler fuer pathname:" + current + ":" + e);
}
}
// A test case for the treatment of drive letters.
// If a drive specifier (a single letter followed by :)
// is provided it is to be replaced according to the platform.
// On windows: d: --> d: (no transformation of drive specifier)
// On other platform: d: --> /d/ or more precisely sep + "d" + sep
public void test_TreatmentOfDriveLetters_01() {
AudioFile af = new AudioFile();
af.parsePathname("Z:\\part1\\\\file.mp3\\");
char sep = java.io.File.separatorChar;
if (isWindows()) {
// On Windows we expect "Z:\part1\file.mp3\"
assertEquals("Pathname stored incorrectly", "Z:" + sep + "part1"
+ sep + "file.mp3" + sep, af.getPathname());
} else {
// On other platforms we expect "/Z/part1/file.mp3/"
assertEquals("Pathname stored incorrectly", sep + "Z" + sep
+ "part1" + sep + "file.mp3" + sep, af.getPathname());
}
assertEquals("Returned filename is incorrect", "", af.getFilename());
}
// A test case for the treatment of drive letters
public void test_TreatmentOfDriveLetters_02() {
AudioFile af = new AudioFile();
af.parsePathname("Z:///part1//file.mp3");
char sep = java.io.File.separatorChar;
if (isWindows()) {
// On Windows we expect "Z:\part1\file.mp3"
assertEquals("Pathname stored incorrectly", "Z:" + sep + "part1"
+ sep + "file.mp3", af.getPathname());
} else {
// On other platforms we expect "/Z/part1/file.mp3/"
assertEquals("Pathname stored incorrectly", sep + "Z" + sep
+ "part1" + sep + "file.mp3", af.getPathname());
}
assertEquals("Returned filename is incorrect", "file.mp3",
af.getFilename());
}
// A test case for the treatment of drive letters
public void test_TreatmentOfDriveLetters_03() {
AudioFile af = new AudioFile();
af.parsePathname("Z:///file.mp3");
char sep = java.io.File.separatorChar;
if (isWindows()) {
// On Windows we expect "Z:\file.mp3"
assertEquals("Pathname stored incorrectly",
"Z:" + sep + "file.mp3", af.getPathname());
} else {
// On other platforms we expect "/Z/file.mp3"
assertEquals("Pathname stored incorrectly", sep + "Z" + sep
+ "file.mp3", af.getPathname());
}
assertEquals("Returned filename is incorrect", "file.mp3",
af.getFilename());
}
/*--------------------------------------------------------------------------
* Auxiliary methods
*/
private boolean isWindows() {
return System.getProperty("os.name").toLowerCase().indexOf("win") >= 0;
}
}
Zuletzt bearbeitet: