Bonjour tout le monde, j'ai un problème avec ma classe TimeCut. TimeCut représente un temps (heures, minutes, secondes) et cette classe est immuable.
J'ai une deuxième classe Time, qui celle ci à le même but mais n'est pas immuable.
Je voulais avoir un constructeur avec en paramètre Time pour la classe TimeCut, mais j'obtient les erreurs suivantes :
Ce sont ces lignes qui poses problèmes :Code:
1
2
3
4
5
6
7 Exception in thread "main" Exception in thread "Thread-0" java.lang.NullPointerException at time.TimeCut.<init>(TimeCut.java:39) at time.Chrono.now(Chrono.java:34) at time.Test.main(Test.java:10) java.lang.NullPointerException at time.Chrono.run(Chrono.java:16)
Je veux tester un Chrono mais pour le coup je suis pas prêt de pouvoir, je comprend pas du tout d'où vient mon erreur.Code:
1
2
3
4
5
6
7 public TimeCut( Time t ) { this.seconds = new Long(t.seconds()); this.minutes = new Long(t.minutes()); this.hours = new Long(t.hours()); }
TimeCut
TimeCode:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 package time; public final class TimeCut { public TimeCut( long seconds ) { this.seconds = seconds; this.minutes = 0; this.hours = 0; } public TimeCut( long seconds, long minutes ) { this.seconds = seconds; this.minutes = minutes; this.hours = 0; } public TimeCut( long seconds, long minutes, long hours ) { this.seconds = new Long(seconds); this.minutes = new Long(minutes); this.hours = new Long(hours); } public TimeCut( TimeCut tc ) { this.seconds = tc.seconds(); this.minutes = tc.minutes(); this.hours = tc.hours(); } public TimeCut( Time t ) { this.seconds = new Long(t.seconds()); this.minutes = new Long(t.minutes()); this.hours = new Long(t.hours()); } public long seconds() {return new Long(this.seconds);} public long minutes() {return new Long(this.minutes);} public long hours() {return new Long(this.hours);} private final long seconds; private final long minutes; private final long hours; }
ChronoCode:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75 package time; /** * @author Xyliaris * */ public class Time { public Time( long seconds ) { this.seconds = seconds; } public Time( long seconds, long minutes ) { this.seconds = seconds; this.minutes = minutes; } public Time( Time t ) { this.seconds = t.seconds(); this.minutes = t.minutes(); this.hours = t.hours(); } public Time( TimeCut tc ) { this.seconds = tc.seconds(); this.minutes = tc.minutes(); this.hours = tc.hours(); } public void recompose() { if( this.seconds > 59 ) { this.seconds = (long)(((float)(this.seconds) / 60 - this.seconds / 60 ) * 60 ); this.minutes += this.seconds / 60; } if( this.minutes > 59 ) { this.minutes = (long)(((float)(this.minutes) / 60 - this.minutes / 60 ) * 60 ); this.hours += this.minutes / 60; } if( this.hours > 24 ) { this.hours = 24; } } public void addTime( Time t ) { this.seconds += t.seconds(); this.minutes += t.minutes(); this.hours += t.hours(); } public void addTime( TimeCut tc ) { this.seconds += tc.seconds(); this.minutes += tc.minutes(); this.hours += tc.hours(); } public long seconds() {return this.seconds;} public long minutes() {return this.minutes;} public long hours() {return this.hours;} private long seconds; private long minutes; private long hours; }
Vous pourriez m'aider ? :)Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 package time; public class Chrono extends Thread { public Chrono( boolean alwaysRun ) { this.alwaysRun = new Boolean(alwaysRun); } public void run() { this.isStopped = false; while(!this.isStopped || this.alwaysRun) { this.time_now.addTime(new TimeCut(1)); try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();} } } public void alwaysRun( boolean value ) { this.alwaysRun = value; } public boolean alwaysRun() { return new Boolean(this.alwaysRun); } public TimeCut now() { return new TimeCut(this.time_now); } public void reset() { this.isStopped = true; this.time_now = new Time(0); } private boolean isStopped; private boolean alwaysRun; private Time time_now; }