解密时出现空指针异常

时间:2016-12-11 20:56:10

标签: java loops exception encryption

我正在使用一个名为decrypt的函数来解密我使用bufferedreader在文件中加密的字符串,但问题是当我使用它抛出NullPointerException的函数时,任何人都可以帮助我吗?提前谢谢!

这是我的功能:

public  String Decrypt (String Word,int key)
{
   String result="";
   for (int i=0;i<Word.length();i++)
    {
        result+=(char)(Word.charAt(i)-key);
    }
   return result;
}

bufferedreader代码:

 try {
       BufferedReader out=new BufferedReader(new FileReader("array.txt"));
       String line="";
       while((line=Decrypt(out.readLine(), 30)) !=null) // unknown exception
       {
         output.append("Your String is: \n"+ line);
       }
     }catch (FileNotFoundException ex) {

          } catch (IOException ex) {

          }

1 个答案:

答案 0 :(得分:1)

这不干净:

<header>
<nav>
    <h1> Welcome to Mommy Madness</h1>
    <ul>
        <li class="parentMenu"><a href="../Home/mmHome.html">Home</a>
            <ul class="sub-menu">
                <li><a href="../Home/mmAbout.html">About</a></li>
                <li><a href="../Home/mmContact.html">Contact</a></li>
            </ul>
        </li>
        <li class="parentMenu"><a href="../PregnancyPages/preg.html">Pregnancy</a>
            <!--
            Gender Predictions:
            Old Wive's Tale
            Boy vs Girl- The Ramzi Method
            -->
            <ul class="sub-menu">
                <li><a href="../PregnancyPages/pregAdvice.html">Advice</a></li>
                <li><a href="../PregnancyPages/pregGenderPredictions.html">Gender Predictions</a></li>
                <li><a href="../PregnancyPages/pregTTC.html">Trying To Conceive</a></li>
            </ul>
        </li>
        <li class="parentMenu"><a href="../AllAboutBaby/AllAboutBaby.html">All About Baby</a>
            <ul class="sub-menu">
                <li><a href="../AllAboutBaby/aabFetalDev.html">Fetal Development</a></li>
                <li><a href="../AllAboutBaby/aabGuidelines.html">Guidelines </a> </li>
                <li><a href="../AllAboutBaby/aabMilestones.html"> Milestones</a></li>
            </ul>
        </li>
        <li class="parentMenu"><a href="../PartyMomma/PartyMomma.html">Party Momma</a>
            <!--
            Birthdays - Link to 1-10th bdays.
            -->
            <ul class="sub-menu">
                <li><a href="../PartyMomma/pmPregAnn.html">Pregnancy Announcement</a></li>
                <li><a href="../PartyMomma/pmGenderReveal.html">Gender Reveal</a></li>
                <li><a href="../PartyMomma/pmBabyShower.html">Baby Shower</a></li>
                <li><a href="../PartyMomma/pmBirthAnn.html">Birth Announcement</a></li>
                <li><a href="../PartyMomma/pmBirthdays.html"> Birthdays</a></li>
            </ul>
        </li>
        <li class="parentMenu"><a href="../Stations/Stations.html">Stations</a>
            <ul class="sub-menu">
                <li><a href="../Stations/sHospitalBag.html">Hospital Bag</a></li>
                <li><a href="../Stations/sDiaperBag.html">Diaper Bag</a></li>
                <li><a href="../Stations/sChangingStation.html">Changing Station</a></li>
                <li><a href="../Stations/sBabyGear.html">Baby Gear</a></li>
            </ul>
        </li>
        <li class="parentMenu"><a href="../MemoryMarkers/memoryMarkers.html">Memory Markers</a>
            <!--
            Drop Down Menu:
            DIY
            Purchases
            (Both to have holiday/event selectors on right of page)
            -->
            <ul class="sub-menu">
                <li><a href="../MemoryMarkers/memDIY.html">DIY</a></li>
                <li><a href="../MemoryMarkers/memPurchase.html">Purchases</a></li>
            </ul>
        </li>
        <li class="parentMenu"><a href="../Reviews/Reviews.html">Reviews</a>
            <ul class="sub-menu">
                <li><a href="../Reviews/Games.html">Games</a></li>
                <li><a href="../Reviews/Gear.html">Gear</a></li>
                <li><a href="../Reviews/Learning.html">Learning</a></li>
            </ul>
        </li>
        <li class="parentMenu"><a href="../Blog/mmBlog.html">Blog</a>
            <ul class="sub-menu">
                <li>Fit Momma</li>
                <li>Minimal Momma</li>
                <li>Modern Momma</li>
                <li>Organic Momma</li>
                <li>Organizing Queen</li>
                <li>Savings Savvy</li>
                <li>Tech Savvy</li>
                <li>Traditional Momma</li>
            </ul>
        </li>
    </ul>
</nav>

我们知道当BufferedReader按照BufferedReader API到达流的末尾时,readline将返回body { background-color: beige; color: lightblue; padding: 0; margin:0; } header { background-color: lightblue; padding: 5px 0; margin: 0; } header h1 { color: cadetblue; font-family: Arial; margin: 0; padding: 5px 15px; display: inline; } .navMenu{ display: inline; margin: 0; } .navMenu .parentMenu { display: inline-block; list-style-type: none; background-color: lightgray; padding: 5px 10px; border: thin solid darkgray; border-radius: 3px; color: honeydew; position: relative; margin: 0; } .navMenu .parentMenu a{ color: azure; } .navMenu .parentMenu .sub-menu{ display: none; } .navMenu .parentMenu:hover .sub-menu{ display: block; position: absolute; list-style-type: none; margin:0; } .parentMenu:hover .sub-menu li{ border: thin solid darkgray; padding: 4%; background-color: lightgray; color: honeydew; text-align: left; white-space: nowrap; width: 100%; list-style-type: none; margin: 0; } .parentMenu .sub-menu li:hover { background-color: lightsteelblue; } .section { background-color: wheat; color: darkslategray; padding: 5px; float: left; display: inline; width: 63%; margin: 0 1% 1% 1%; border-radius: 10px; border: thin solid khaki; box-shadow: lightgray; } #about { float: right; width: 30%; margin: 1% 1% 0 0; text-align: center; } #home{ margin: 1% 0 1% 1%; } h4, h3 { color: lightseagreen; } ,但是您的代码没有正确处理这个,即您的Decrypt方法将调用{对Word参数的{1}}方法是否为null。我建议你不要试图让你的代码过于简短,而是将阅读器的阅读单独分开。如,

while((line=Decrypt(out.readLine(), 30)) !=null)

另外,您需要学习并使用Java naming conventions。变量名都应以较低的字母开头,而类名以大写字母开头。了解并遵循这一点将使我们能够更好地理解您的代码,并使您能够更好地理解其他代码。

除了2:不要将你的异常捕获块留空,因为这是Java相当于你的汽车闭上眼睛。至少打印出堆栈跟踪并明确查看Exceptions Tutorial