По существу, в строке 331 игрока, я получаю ошибку исключения нулевого указателя из-за того, что функциональная карта возвращает нулевое значение вместо целочисленного значения []. Это объект игрока в игре.
Я старался изо всех сил, что только мог придумать, и, похоже, ничего не работает, я даже не уверен в проблеме и в том, как ее исправить.
package Default;
import java.util.LinkedList;
import java.util.Random;
import Default.Equipment;
import Default.EquipmentType;
public class Player implements PlayerObjectInterface
{
//-------------------------------------------------------------------------------------
// Class Variables
//-------------------------------------------------------------------------------------
EquipmentMap EquipmentList;
public enum CharacterClass{
Warrior, Crusader, Rogue, Paladin};
//00 01 02 03 04 01 02 03 04 05
private String szCharacterName;
private CharacterClass eCharacterClass;
private Equipment[] myEquipment;
private int iExperience;
private int[] iIVs;
private int iCurrentHitpoints;
//private Inventory
//-------------------------------------------------------------------------------------
// Constructor
//-------------------------------------------------------------------------------------
//default
public Player()
{
myEquipment = new Equipment[5];
iIVs = new int[3];
EquipmentList = new EquipmentMap();
reset();
return;
}
//creating new Player at game start
public Player(String CharacterName, CharacterClass CharacterClass)
{
myEquipment = new Equipment[5];
iIVs = new int[3];
EquipmentList = new EquipmentMap();
reset();
setCharacterName(CharacterName);
setCharacterClass(CharacterClass);
return;
}
//loading Player from file
//-------------------------------------------------------------------------------------
// Setters
//-------------------------------------------------------------------------------------
public void setCharacterName(String CharacterName)
{
this.szCharacterName = CharacterName;
return;
}
public void setCharacterClass(CharacterClass Class)
{
this.eCharacterClass = Class;
return;
}
public void setMyEquipment(EquipmentType equipmentType, ItemType Item)
{
this.myEquipment[ equipmentType.getValue() ] .setItem(Item) ;
return;
}
public void setExperience(int Experience)
{
this.iExperience = Experience;
return;
}
private void setIV(IVType IVType, int IV)
{
this.iIVs[ IVType.getValue() ] = IV;
return;
}
private void setCurrentHitpoints(int CurrentHitpoints)
{
this.iCurrentHitpoints = CurrentHitpoints;
if(getCurrentHitpoints() < 0)
{
setCurrentHitpoints(0);
}
return;
}
//-------------------------------------------------------------------------------------
// Getters
//-------------------------------------------------------------------------------------
public String getCharacterName()
{
return(this.szCharacterName);
}
public CharacterClass getCharacterClass()
{
return(this.eCharacterClass);
}
public ItemType getMyEquipment(EquipmentType equipmentType)
{
return( (this.myEquipment[ equipmentType.getValue() ].getItem()) );
}
public int getExperience()
{
return(this.iExperience);
}
public int getIV(BaseStatType BaseStatType)
{
return(this.iIVs[ BaseStatType.getValue() ]);
}
private int getCurrentHitpoints()
{
return(this.iCurrentHitpoints);
}
private int getLevel()
{
int iLevel = 5;
int iExperience = getExperience();
iLevel = (int) java.lang.Math.floor( ( Math.pow( iExperience,-3 ) / 1.25 ) );
if( iLevel > 100 )
{
iLevel = 100;
}
return (iLevel);
}
private int getBaseStat(BaseStatType eBaseStatType)
{
int[] iWarriorBaseStats = {60,100,80};
int[] iCrusaderBaseStats = {80,60,100};
int[] iPaladinBaseStats = {60,80,100};
int[] iRogueBaseStats = {100,80,60};
int[] iDefaultBaseStats = {60,60,60};
int iBaseStat = 60;
CharacterClass eClass = getCharacterClass();
switch(eClass)
{
case Warrior:
iBaseStat = iWarriorBaseStats[ eBaseStatType.getValue() ];
break;
case Crusader:
iBaseStat = iCrusaderBaseStats[ eBaseStatType.getValue() ];
break;
case Paladin:
iBaseStat = iPaladinBaseStats[ eBaseStatType.getValue() ];
break;
case Rogue:
iBaseStat = iRogueBaseStats[ eBaseStatType.getValue() ];
break;
default:
iBaseStat = iDefaultBaseStats[ eBaseStatType.getValue() ];
break;
}
return (iBaseStat);
}
public int getStat(StatType eStatType)
{
int iStat = 0;
int iBaseStat = 60;
int iLevel = getLevel();
int iIV = 0;
switch(eStatType)
{
case Attack:
iBaseStat = getBaseStat(BaseStatType.Strength);
iIV = getIV(BaseStatType.Strength);
break;
case Defence:
iBaseStat = getBaseStat(BaseStatType.Protection);
iIV = getIV(BaseStatType.Protection);
break;
case Hitpoints:
iBaseStat = getBaseStat(BaseStatType.Vitality);
iIV = getIV(BaseStatType.Vitality);
break;
default:
iBaseStat = 60;
iIV = 0;
break;
}
if (eStatType == StatType.Hitpoints)
{
iStat = ( ( ( ( ( 2 * iBaseStat ) + iIV )
* iLevel ) / 100 ) + iLevel + 10);
}
else
{
iStat = ( ( ( ( ( 2 * iBaseStat) + iIV )
* iLevel ) / 100 ) + 5);
}
return(iStat);
}
private double getModifier()
{
Random RandomModifier = new Random();
int iRandom1 = 0;
int iRandom2 = 0;
int iMinimumRandom = 0;
int iMaximumRandom = 0;
double dModifier = 1;
double dCriticalModifier = 1;
double dRandomModifier = 1;
Integer iSwordStats[] = EquipmentList.getObject(myEquipment[ (EquipmentType.Sword).getValue() ]);
int iCritChance = 5 + iSwordStats[ (WeaponStatType.CritChance.getValue()) ];
iMinimumRandom = 1;
iMaximumRandom = 100;
iRandom1 = (RandomModifier.nextInt
( (iMaximumRandom - iMinimumRandom) + 1 )
+ iMinimumRandom );
if( iRandom1 <= iCritChance )
{
dCriticalModifier = 1.75;
}
else
{
dCriticalModifier = 1;
}
iMinimumRandom = 85;
iMaximumRandom = 100;
iRandom2 = (RandomModifier.nextInt
( (iMaximumRandom - iMinimumRandom) + 1 )
+ iMinimumRandom );
dRandomModifier = iRandom2 / 100;
dModifier = dCriticalModifier * dRandomModifier;
return (dModifier);
}
public int getPlayerAttack()
{
int iPlayerAttack;
Integer iSwordStats[] = EquipmentList.getObject(myEquipment[ (EquipmentType.Sword).getValue() ]);
iPlayerAttack = getStat(StatType.Attack)
+ iSwordStats[ (WeaponStatType.Power).getValue() ];
return(iPlayerAttack);
}
public int getPlayerDefence()
{
int iPlayerDefence;
Integer iHelmetStats[] = EquipmentList.getObject(myEquipment[ (EquipmentType.Helmet).getValue() ]);
Integer iChestplateStats[] = EquipmentList.getObject(myEquipment[ (EquipmentType.Chestplate).getValue() ]);
Integer iLeggingsStats[] = EquipmentList.getObject(myEquipment[ (EquipmentType.Leggings).getValue() ]);
Integer iBootsStats[] = EquipmentList.getObject(myEquipment[ (EquipmentType.Boots).getValue() ]);
iPlayerDefence = getStat(StatType.Defence)
+ iHelmetStats[ (ArmourStatType.Power).getValue() ]
+ iChestplateStats[ (ArmourStatType.Power).getValue() ]
+ iLeggingsStats[ (ArmourStatType.Power).getValue() ]
+ iBootsStats[ (ArmourStatType.Power).getValue() ];
return(iPlayerDefence);
}
public int getPlayerHitpoints()
{
int iPlayerHitpoints = 0;
Integer[] iHelmetStats = new Integer[2];
Integer[] iChestplateStats = new Integer[2];
Integer[] iLeggingsStats = new Integer[2];
Integer[] iBootsStats = new Integer[2];
Equipment[] eMyEquipment = new Equipment[5];
eMyEquipment[0] = new Equipment(EquipmentType.Sword , getMyEquipment(EquipmentType.Sword) );
eMyEquipment[0] = new Equipment(EquipmentType.Helmet , getMyEquipment(EquipmentType.Helmet) );
eMyEquipment[0] = new Equipment(EquipmentType.Chestplate , getMyEquipment(EquipmentType.Chestplate) );
eMyEquipment[0] = new Equipment(EquipmentType.Leggings , getMyEquipment(EquipmentType.Leggings) );
eMyEquipment[0] = new Equipment(EquipmentType.Boots , getMyEquipment(EquipmentType.Boots) );
iHelmetStats = EquipmentList.getObject( eMyEquipment[ (EquipmentType.Helmet).getValue() ]);
iChestplateStats = EquipmentList.getObject( eMyEquipment[ (EquipmentType.Chestplate).getValue() ]);
iLeggingsStats = EquipmentList.getObject( eMyEquipment[ (EquipmentType.Leggings).getValue() ]);
iBootsStats = EquipmentList.getObject( eMyEquipment[ (EquipmentType.Boots).getValue() ]);
iPlayerHitpoints = getStat(StatType.Hitpoints)
+ iHelmetStats[ (ArmourStatType.Hitpoints).getValue() ]
+ iChestplateStats[ (ArmourStatType.Hitpoints).getValue() ]
+ iLeggingsStats[ (ArmourStatType.Hitpoints).getValue() ]
+ iBootsStats[ (ArmourStatType.Hitpoints).getValue() ];
return(iPlayerHitpoints);
}
//-------------------------------------------------------------------------------------
// Utilities
//-------------------------------------------------------------------------------------
public void addExperience(int Experience)
{
int iLevelBefore = getLevel();
int iLevelAfter = 0;
setExperience( (getExperience() + Experience) );
iLevelAfter = getLevel();
if(iLevelBefore < iLevelAfter)
{
System.out.println("You leveled up to Level " + iLevelAfter);
}
return;
}
public boolean isPlayerDead()
{
if(getCurrentHitpoints() == 0)
{
return(true);
}
return(false);
}
public int calculatePlayerDamage(int iEnemyDefence)
{
double dDamage = 0;
int iDamage = 0;
int iPlayerAttack = getPlayerAttack();
int iLevel = getLevel();
double dModifier = getModifier();
//finish calculation
dDamage = (((((( 2 * iLevel) / 5 ) * (iPlayerAttack / iEnemyDefence ) ) / 50 ) + 2) * dModifier);
iDamage = (int) java.lang.Math.floor(dDamage);
return(iDamage);
}
public void displayObject()
{
System.out.println("CharacterName = " + getCharacterName());
System.out.println("Class = " + getCharacterClass());
System.out.println("Equipment (Sword) = " + getMyEquipment(EquipmentType.Sword));
System.out.println("Equipment (Helmet) = " + getMyEquipment(EquipmentType.Helmet));
System.out.println("Equipment (Chestplate) = " + getMyEquipment(EquipmentType.Chestplate));
System.out.println("Equipment (Leggings) = " + getMyEquipment(EquipmentType.Leggings));
System.out.println("Equipment (Boots) = " + getMyEquipment(EquipmentType.Boots));
System.out.println("Experience = " + getExperience());
System.out.println("Strength IV = " + getIV(BaseStatType.Strength));
System.out.println("Defence IV = " + getIV(BaseStatType.Protection));
System.out.println("Vitality IV = " + getIV(BaseStatType.Vitality));
System.out.println("Attack = " + getPlayerAttack());
System.out.println("Defence = " + getPlayerDefence());
System.out.println("Hitpoints = " + getCurrentHitpoints() + "/" + getPlayerHitpoints());
//class equipment experience IV hitpoints
}
private void reset()
{
Random RandomIV = new Random();
int iMinimumIV = 0;
int iMaximumIV = 31;
setCharacterName("A");
setCharacterClass(CharacterClass.Warrior);
this.myEquipment[0] = new Equipment(EquipmentType.Sword , ItemType.Beginner_Sword);
this.myEquipment[1] = new Equipment(EquipmentType.Helmet , ItemType.Beginner_Helmet);
this.myEquipment[2] = new Equipment(EquipmentType.Chestplate , ItemType.Beginner_Chestplate);
this.myEquipment[3] = new Equipment(EquipmentType.Leggings , ItemType.Beginner_Leggings);
this.myEquipment[4] = new Equipment(EquipmentType.Boots , ItemType.Beginner_Boots);
setExperience(3907);
setIV( (IVType.Strength), RandomIV.nextInt
( (iMaximumIV - iMinimumIV) + 1 )
+ iMinimumIV);
setIV( (IVType.Protection), RandomIV.nextInt
( (iMaximumIV - iMinimumIV) + 1 )
+ iMinimumIV);
setIV( (IVType.Vitality), RandomIV.nextInt
( (iMaximumIV - iMinimumIV) + 1 )
+ iMinimumIV);
populateEquipmentMap();
setCurrentHitpoints(getPlayerHitpoints());
}
private void populateEquipmentMap()
{
Integer[][] passingvalues =
{
{15,0},{35,8},{75,16},{110,25},{150,32},{185,40},
{10,16},{22,35},{47,72},{69,107},{94,144},{116,182},
{15,25},{35,55},{75,115},{110,170},{150,230},{185,290},
{14,22},{31,49},{49,101},{97,149},{132,202},{162,254},
{8,13},{18,28},{38,58},{55,85},{75,115},{93,145}
};
Equipment[] myEquipment = new Equipment[30];
myEquipment[0] = new Equipment(EquipmentType.Sword, ItemType.Beginner_Sword);
myEquipment[1] = new Equipment(EquipmentType.Sword, ItemType.Cloth_Sword);
myEquipment[2] = new Equipment(EquipmentType.Sword, ItemType.Wooden_Sword);
myEquipment[3] = new Equipment(EquipmentType.Sword, ItemType.Iron_Sword);
myEquipment[4] = new Equipment(EquipmentType.Sword, ItemType.Frost_Sword);
myEquipment[5] = new Equipment(EquipmentType.Sword, ItemType.Magma_Sword);
myEquipment[6] = new Equipment(EquipmentType.Helmet, ItemType.Beginner_Helmet);
myEquipment[7] = new Equipment(EquipmentType.Helmet, ItemType.Cloth_Helmet);
myEquipment[8] = new Equipment(EquipmentType.Helmet, ItemType.Wooden_Helmet);
myEquipment[9] = new Equipment(EquipmentType.Helmet, ItemType.Iron_Helmet);
myEquipment[10] = new Equipment(EquipmentType.Helmet, ItemType.Frost_Helmet);
myEquipment[11] = new Equipment(EquipmentType.Helmet, ItemType.Magma_Helmet);
myEquipment[12] = new Equipment(EquipmentType.Chestplate, ItemType.Beginner_Chestplate);
myEquipment[13] = new Equipment(EquipmentType.Chestplate, ItemType.Cloth_Chestplate);
myEquipment[14] = new Equipment(EquipmentType.Chestplate, ItemType.Wooden_Chestplate);
myEquipment[15] = new Equipment(EquipmentType.Chestplate, ItemType.Iron_Chestplate);
myEquipment[16] = new Equipment(EquipmentType.Chestplate, ItemType.Frost_Chestplate);
myEquipment[17] = new Equipment(EquipmentType.Chestplate, ItemType.Magma_Chestplate);
myEquipment[18] = new Equipment(EquipmentType.Leggings, ItemType.Beginner_Leggings);
myEquipment[19] = new Equipment(EquipmentType.Leggings, ItemType.Cloth_Leggings);
myEquipment[20] = new Equipment(EquipmentType.Leggings, ItemType.Wooden_Leggings);
myEquipment[21] = new Equipment(EquipmentType.Leggings, ItemType.Iron_Leggings);
myEquipment[22] = new Equipment(EquipmentType.Leggings, ItemType.Frost_Leggings);
myEquipment[23] = new Equipment(EquipmentType.Leggings, ItemType.Magma_Leggings);
myEquipment[24] = new Equipment(EquipmentType.Boots, ItemType.Beginner_Boots);
myEquipment[25] = new Equipment(EquipmentType.Boots, ItemType.Cloth_Boots);
myEquipment[26] = new Equipment(EquipmentType.Boots, ItemType.Wooden_Boots);
myEquipment[27] = new Equipment(EquipmentType.Boots, ItemType.Iron_Boots);
myEquipment[28] = new Equipment(EquipmentType.Boots, ItemType.Frost_Boots);
myEquipment[29] = new Equipment(EquipmentType.Boots, ItemType.Magma_Boots);
EquipmentList.addObject(myEquipment[0] , passingvalues[0]);
EquipmentList.addObject(myEquipment[1] , passingvalues[1]);
EquipmentList.addObject(myEquipment[2] , passingvalues[2]);
EquipmentList.addObject(myEquipment[3] , passingvalues[3]);
EquipmentList.addObject(myEquipment[4] , passingvalues[4]);
EquipmentList.addObject(myEquipment[5] , passingvalues[5]);
EquipmentList.addObject(myEquipment[6] , passingvalues[6]);
EquipmentList.addObject(myEquipment[7] , passingvalues[7]);
EquipmentList.addObject(myEquipment[8] , passingvalues[8]);
EquipmentList.addObject(myEquipment[9] , passingvalues[9]);
EquipmentList.addObject(myEquipment[10] , passingvalues[10]);
EquipmentList.addObject(myEquipment[11] , passingvalues[11]);
EquipmentList.addObject(myEquipment[12] , passingvalues[12]);
EquipmentList.addObject(myEquipment[13] , passingvalues[13]);
EquipmentList.addObject(myEquipment[14] , passingvalues[14]);
EquipmentList.addObject(myEquipment[15] , passingvalues[15]);
EquipmentList.addObject(myEquipment[16] , passingvalues[16]);
EquipmentList.addObject(myEquipment[17] , passingvalues[17]);
EquipmentList.addObject(myEquipment[18] , passingvalues[18]);
EquipmentList.addObject(myEquipment[19] , passingvalues[19]);
EquipmentList.addObject(myEquipment[20] , passingvalues[20]);
EquipmentList.addObject(myEquipment[21] , passingvalues[21]);
EquipmentList.addObject(myEquipment[22] , passingvalues[22]);
EquipmentList.addObject(myEquipment[23] , passingvalues[23]);
EquipmentList.addObject(myEquipment[24] , passingvalues[24]);
EquipmentList.addObject(myEquipment[25] , passingvalues[25]);
EquipmentList.addObject(myEquipment[26] , passingvalues[26]);
EquipmentList.addObject(myEquipment[27] , passingvalues[27]);
EquipmentList.addObject(myEquipment[28] , passingvalues[28]);
EquipmentList.addObject(myEquipment[29] , passingvalues[29]);
}
//-------------------------------------------------------------------------------------
// Test-Rig
//-------------------------------------------------------------------------------------
public static void main(String[] args)
{
Player player = new Player();
player.displayObject();
/**
EquipmentMap EquipmentList = new EquipmentMap();
Equipment equipment = new Equipment(EquipmentType.Sword,ItemType.Beginner_Sword);
Integer i[] = {20,10};
EquipmentList.addObject(equipment, i);
Equipment equip = new Equipment(EquipmentType.Sword,ItemType.Beginner_Sword);
Integer j[] = EquipmentList.getObject(equip);
System.out.println(j[0] + "/" + j[1]);
*/
}
}
package Default;
import Default.EquipmentType;
public class Equipment
{
private EquipmentType eEquipmentType;
private ItemType eItem;
public Equipment(EquipmentType EquipmentType, ItemType ItemType)
{
setEquipmentType(EquipmentType);
reset();
setItem(ItemType);
}
public Equipment(EquipmentType EquipmentType)
{
setEquipmentType(EquipmentType);
reset();
}
//item ids are as follows
//Sword: 10-19
//Helmet: 20-29
//Chestplate: 30-39
//Leggings: 40-49
//Boots: 50-59
//0 sword, 1 helmet, 2 chestplate, 3 leggings, 4 boots
public void setEquipmentType(EquipmentType EquipmentType)
{
this.eEquipmentType = EquipmentType;
}
public void setItem(ItemType Item)
{
this.eItem = Item;
}
public EquipmentType getEquipmentType()
{
return( this.eEquipmentType );
}
public ItemType getItem()
{
return( this.eItem );
}
private void reset()
{
switch(getEquipmentType())
{
case Sword:
setItem(ItemType.Beginner_Sword);
break;
case Helmet:
setItem(ItemType.Beginner_Helmet);
break;
case Chestplate:
setItem(ItemType.Beginner_Chestplate);
break;
case Leggings:
setItem(ItemType.Beginner_Leggings);
break;
case Boots:
setItem(ItemType.Beginner_Boots);
break;
}
}
public static void main(String[] args)
{
}
}
package Default;
import java.util.Map;
import java.util.HashMap;
public class EquipmentMap
{
//-------------------------------------------------------------------------------------
// Class Variables
//-------------------------------------------------------------------------------------
Map<Equipment, Integer[]> EquipmentMap;
//-------------------------------------------------------------------------------------
// Constructor
//-------------------------------------------------------------------------------------
EquipmentMap()
{
EquipmentMap = new HashMap < Equipment , Integer[] > ();
return;
}
//-------------------------------------------------------------------------------------
// Setters
//-------------------------------------------------------------------------------------
public void addObject( Object Equipment , Integer[] i)
{
EquipmentMap.put( ( Default.Equipment ) Equipment , i ) ;
return ;
}
//-------------------------------------------------------------------------------------
// Getters
//-------------------------------------------------------------------------------------
public Integer[] getObject( Object Equipment )
{
Integer[] i = EquipmentMap.get( ( Default.Equipment ) Equipment ) ;
return ( i ) ;
}
//-------------------------------------------------------------------------------------
// Test-Rig
//-------------------------------------------------------------------------------------
public static void main(String[] args)
{
}
}
package Default;
public enum ArmourStatType
{
Power(0), Hitpoints(1);
private int iValue;
private ArmourStatType( int Value )
{
this.iValue = Value ;
return ;
}
// access method
public int getValue()
{
return ( this.iValue ) ;
}
}
package Default;
public enum WeaponStatType
{
Power(0), CritChance(1);
private int iValue;
private WeaponStatType( int Value )
{
this.iValue = Value ;
return ;
}
// access method
public int getValue()
{
return ( this.iValue ) ;
}
}
package Default;
public enum BaseStatType
{
Strength(0), Protection(1), Vitality(2);
private int iValue;
private BaseStatType( int Value )
{
this.iValue = Value ;
return ;
}
// access method
public int getValue()
{
return ( this.iValue ) ;
}
}
package Default;
public enum StatType
{
Attack(0), Defence(1), Hitpoints(2);
private int iValue;
private StatType( int Value )
{
this.iValue = Value ;
return ;
}
// access method
public int getValue()
{
return ( this.iValue ) ;
}
}
package Default;
public enum ItemType
{
Beginner_Sword(10),
Beginner_Helmet(20),
Beginner_Chestplate(30),
Beginner_Leggings(40),
Beginner_Boots(50),
Cloth_Sword(11),
Cloth_Helmet(21),
Cloth_Chestplate(31),
Cloth_Leggings(41),
Cloth_Boots(51),
Wooden_Sword(12),
Wooden_Helmet(22),
Wooden_Chestplate(32),
Wooden_Leggings(42),
Wooden_Boots(52),
Iron_Sword(13),
Iron_Helmet(23),
Iron_Chestplate(33),
Iron_Leggings(43),
Iron_Boots(53),
Frost_Sword(14),
Frost_Helmet(24),
Frost_Chestplate(34),
Frost_Leggings(44),
Frost_Boots(54),
Magma_Sword(15),
Magma_Helmet(25),
Magma_Chestplate(35),
Magma_Leggings(45),
Magma_Boots(55);
private int iID;
private ItemType( int ID )
{
this.iID = ID ;
return ;
}
// access method
public int getValue()
{
return ( this.iID ) ;
}
}
package Default;
public enum IVType
{
Strength(0), Protection(1), Vitality(2);
private int iValue;
private IVType( int Value )
{
this.iValue = Value ;
return ;
}
// access method
public int getValue()
{
return ( this.iValue ) ;
}
}
Ожидаемым результатом должно быть правильное выполнение функций сброса, и вычисление в getPlayerHitpoints может быть продолжено из-за возврата целого числа [].
Это сообщение об ошибке: https://gyazo.com/d509055b1d7e5d55386cbee684ac3531